PC 导航 鼠标滚动效果与鼠标拖拽效果实现

 

 <div class="tab-wrap right">
      <ul class="tab-title">
          <li class="active"><span>热歌榜单</span></li>
          <li><span>飙升榜</span></li>
          <li><span>励志</span></li>
          <li><span>甜蜜物语</span></li>
          <li><span>欧美</span></li>
          <li><span>流行</span></li>
          <li><span>原创</span></li>
          <li><span>国风</span></li>
          <li><span>不想长大</span></li>
          <li><span>翻唱</span></li>
          <li><span>激萌</span></li>
          <li><span>说唱</span></li>
          <li><span>民谣</span></li>
          <li><span>纯音乐</span></li>
      </ul>
 </div>
.tab-wrap {
    width:600px;
    margin: 10px auto 16px;
    overflow:hidden;
    position: relative;
}

.tab-wrap.left:before {
    content: '...';
    width: 34px;
    height: 53px;
    line-height: 45px;
    text-align: center;
    display: block;
    box-shadow: 2px 0px 15px rgba(126,117, 117, 0.14);
    position: absolute;
    top: 0;
    left: 0;
    background-color: #fff;
    z-index: 1;
}

.tab-wrap.right:after {
    content: '...';
    width: 34px;
    height: 53px;
    line-height: 45px;
    text-align: center;
    display: block;
    box-shadow: -2px 0px 15px rgba(126,117, 117, 0.14);
    position: absolute;
    top: 0;
    right: 0;
    background-color: #fff;
}

.tab-title {
    width: 600px;
    display: flex;
    word-break: keep-all;
    margin: auto;
    overflow-x: auto;
    position: relative;
    -webkit-user-select: none;
    transition: transform 0.5s ease;
}
.tab-title::-webkit-scrollbar {
    width: 0;
    height: 0;
    color: transparent;
}

.tab-title li {
    line-height: 52px;
    padding: 0 18px;
    border-bottom: 1px solid #eee;
}

.tab-title li span {
    cursor: pointer;
    position: relative;
}

.tab-title li span:hover {
    color: #1E62EC;
}

.tab-title li.active span {
    color: #1E62EC;
    font-weight: bold;
    cursor: pointer;
    display: block;
}

.tab-title li.active span:before {
    content: '';
    width: 100%;
    height: 2px;
    background-color: #1E62EC;
    position: absolute;
    bottom: 0px;
}
let tab = document.querySelector(".tab-title");

 //音乐 导航滚轮效果
tab.addEventListener("wheel", function (e) {
     let dundong = -e.wheelDelta;
     tab.scrollLeft = tab.scrollLeft + dundong;

     tab.scrollLeft <= 0 ? document.querySelector(".tab-wrap").classList.remove('left') : document.querySelector(".tab-wrap").classList.add('left');
     (tab.scrollLeft + 600) >= tab.scrollWidth ? document.querySelector(".tab-wrap").classList.remove('right') : document.querySelector(".tab-wrap").classList.add('right');
 });


//音乐 导航拖拽效果
tab.addEventListener("mousedown", function (e) {
     var beginMove = e.clientX;

     // 当鼠标移动时,被拖拽元素跟随鼠标移动  onmousemove
     document.onmousemove = function (event) {
           event = event || window.event;
           // 获取鼠标的坐标
           var endMove = event.clientX;
           tab.scrollLeft = tab.scrollLeft + eval(beginMove - endMove);

           tab.scrollLeft <= 0 ? document.querySelector(".tab-wrap").classList.remove('left') : document.querySelector(".tab-wrap").classList.add('left');
           (tab.scrollLeft + 600) >= tab.scrollWidth ? document.querySelector(".tab-wrap").classList.remove('right') : document.querySelector(".tab-wrap").classList.add('right');
      };
      //当鼠标松开时,被拖拽元素固定在当前位置  onmouseup
      document.onmouseup = function (event) {
            document.onmousemove = null;
            // 取消document的onmouseup事件
            document.onmouseup = null;
      }
});

参考博客:js基础 — 简单易懂,拖拽事件(二)_鼠标拖拽事件-CSDN博客

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值