2020年6月23日

导航选中–居中显示

作者 rourou

实现效果:横向导航栏高亮的项,居中显示在屏幕上。(效果如图片)
涉及技术:html、css、jq

实现思路:

1、布局(html+css)

 <p class="tilte">选中默认时:</p>
  <ul class="nav">
    <li class="nav_item">导航一</li>
    <li class="nav_item">导航二</li>
    <li class="nav_item">导航三</li>
    <li class="nav_item nav_item_on">导航四</li>
    <li class="nav_item">导航五</li>
    <li class="nav_item">导航六</li>
  </ul>
  <p class="tilte">选中居中显示时:</p>
  <ul class="nav" id="nav">
    <li class="nav_item">导航一</li>
    <li class="nav_item">导航二</li>
    <li class="nav_item">导航三</li>
    <li class="nav_item nav_item_on">导航四</li>
    <li class="nav_item">导航五</li>
    <li class="nav_item">导航六</li>
  </ul>
html,body,ul,li{padding:0;margin: 0;}
ul{list-style: none;}
.nav{overflow: hidden;white-space: nowrap;overflow-x: auto;overflow-y: hidden;}
.nav_item{display: inline-block;width: 4.5rem;height: 3rem;line-height: 3rem;text-align: center;}
.nav_item_on{color: #f40000;}
.tilte{font-weight: 800;border-left: 3px solid #06b318;margin-left: .5rem;padding-left: .5rem;}

2、js实现居中显示

目标块所移动的区域(A) = 目标块中心屏幕左端(X) – 导航ul宽度的一半(Y) + 导航ul向左滚动的距离(Q)。其中X = 目标块宽度的一半(F) + 目标块距离屏幕左端(Z);

获取方式:
Z:目标块.offset().left
F:目标块.width()/2
Y:导航ul.width()/2
Q:导航ul.get(0).scrollLeft

navCenterShow($('#nav'),'.nav_item_on');
function navCenterShow(item,ChooseItem){
   var chooseLeft =  item.find(ChooseItem).offset().left;//Z
   var chooseHalf = item.find(ChooseItem).width()/2;//F
   var navHalf = item.width()/2;//Y
   var navScrollLeft = item.get(0).scrollLeft;//Q
   item.get(0).scrollLeft = chooseLeft+navScrollLeft+chooseHalf-navHalf
}

完整代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    html,body,ul,li{padding:0;margin: 0;}
    ul{list-style: none;}
    .nav{overflow: hidden;white-space: nowrap;overflow-x: auto;overflow-y: hidden;}
    .nav_item{display: inline-block;width: 4.5rem;height: 3rem;line-height: 3rem;text-align: center;}
    .nav_item_on{color: #f40000;}
    .tilte{font-weight: 800;border-left: 3px solid #06b318;margin-left: .5rem;padding-left: .5rem;}
  </style>
</head>
<body>
  <p class="tilte">选中默认时:</p>
  <ul class="nav">
    <li class="nav_item">导航一</li>
    <li class="nav_item">导航二</li>
    <li class="nav_item">导航三</li>
    <li class="nav_item nav_item_on">导航四</li>
    <li class="nav_item">导航五</li>
    <li class="nav_item">导航六</li>
  </ul>
  <p class="tilte">选中居中显示时:</p>
  <ul class="nav" id="nav">
    <li class="nav_item">导航一</li>
    <li class="nav_item">导航二</li>
    <li class="nav_item">导航三</li>
    <li class="nav_item nav_item_on">导航四</li>
    <li class="nav_item">导航五</li>
    <li class="nav_item">导航六</li>
  </ul>
  <script src="js/jq.js"></script>
  <script>
     navCenterShow($('#nav'),'.nav_item_on');
     function navCenterShow(item,ChooseItem){
       var chooseLeft =  item.find(ChooseItem).offset().left;//Z
       var chooseHalf = item.find(ChooseItem).width()/2;//F
       var navHalf = item.width()/2;//Y
       var navScrollLeft = item.get(0).scrollLeft;//Q
       item.get(0).scrollLeft = chooseLeft+navScrollLeft+chooseHalf-navHalf
    }
  </script>
</body>
</html>