JS实现可用滑块滑动的缓动图代码


Posted in Javascript onSeptember 01, 2019

尝试模仿京东的“发现好货”模块的可用滑块滑动的缓动图

JS代码

function $(id) { return document.getElementById(id); }
//缓动轮播图
var fhTimer;
var fhNum = 0;
var barNum = 0;
fhTimer = setInterval(marquee, 20);
function marquee() {
  fhNum--;
  barNum = fhNum;
  if(fhNum < -2400) {
    fhNum = 0;
  }
  $("fhc_ul").style.left = fhNum + "px";
  if(fhNum < -2400) {
    barNum = fhNum + 2400;
  }
  $("fhc_dBar").style.left = -(barNum / 2.75) + "px";
}
$("fhcShow").onmouseover = function() {
  $("fhc_d_box").style.display = "block";
  clearInterval(fhTimer);
}
$("fhc_d_box").onmouseover = function() {
  $("fhc_d_box").style.display = "block";
}
$("fhcShow").onmouseout = function() {
  $("fhc_d_box").style.display = "none";
  fhTimer = setInterval(marquee, 20);
}
//鼠标悬浮在标题也在暂停滚动
$("fhTit").onmouseover = function() {
  clearInterval(fhTimer);
}
$("fhTit").onmouseout = function() {
  fhTimer = setInterval(marquee, 20);
}
//滑块
$("fhc_dBar").onmousedown = function(event) {
  var event = event || window.event;
  var leftValue = event.clientX - this.offsetLeft;
  document.onmousemove = function(event) {
    var evt = event || window.event;
    var locationX = evt.clientX - leftValue;
    if(locationX < 0) {
      locationX = 0;
    }
    else if(locationX > 960 - 99) {
      locationX = 960 - 99;
    }
    $("fhc_dBar").style.left = locationX + "px";
    fhNum = -locationX * 2.75;
    //如果选中了,就取消选中,防止出现bug
    window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  }
  document.onmouseup = function() {
    document.onmousemove = null;//取消注册的这个事件
  }
}

HTML代码

<div class="fxhh_ctt">
  <div class="fh_c_show" id="fhcShow">
    <ul class="fh_c_under" id="fhc_ul">
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <p class="topTit">商品1</p>
        <img src="" alt="">
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <img src="" alt="">
        <p class="botTit">商品2</p>
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <p class="topTit">商品3</p>
        <img src="" alt="">
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <img src="" alt="">
        <p class="botTit">商品4</p>
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <p class="topTit">商品5</p>
        <img src="" alt="">
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <img src="" alt="">
        <p class="botTit">商品6</p>
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <p class="topTit">商品7</p>
        <img src="" alt="">
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <img src="" alt="">
        <p class="botTit">商品8</p>
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <p class="topTit">商品9</p>
        <img src="" alt="">
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <img src="" alt="">
        <p class="botTit">商品10</p>
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <p class="topTit">商品11</p>
        <img src="" alt="">
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <img src="" alt="">
        <p class="botTit">商品12</p>
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <p class="topTit">商品1</p>
        <img src="" alt="">
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <img src="" alt="">
        <p class="botTit">商品2</p>
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <p class="topTit">商品3</p>
        <img src="" alt="">
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <img src="" alt="">
        <p class="botTit">商品4</p>
      </a></li>
      <li><a href=" " rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
        <p class="topTit">商品5</p>
        <img src="" alt="">
      </a></li>
    </ul>
  </div>
  <!-- 滑块 -->
  <div class="fhc_box" id="fhc_d_box">
    <div class="fhc_drop" id="fhc_dBar"></div>
  </div>
  <!-- 滑块end -->
</div>
CSS代码
.fxhh .fxhh_ctt {
  width: 990px;
  background-color: #fff;
  float: left;
}
.fxhh .fxhh_ctt .fh_c_show .fh_c_under img {
  width: 150px;
  height: 150px;
}
.fxhh .fxhh_ctt .fh_c_show {
  width: 990px;
  height: 260px;
  background-color: #fff;
  overflow: hidden;
  position: relative;
}
.fxhh .fxhh_ctt .fh_c_show .fh_c_under {
  width: 2000%;
  position: absolute;
}
.fxhh .fxhh_ctt .fh_c_show .fh_c_under li {
  float: left;
  text-align: center;
  width: 150px;
  height: 180px;
  margin-top: 40px;
  margin-right: 50px;
}
.fxhh .fxhh_ctt .fh_c_show .fh_c_under li .topTit {
  margin-bottom: 10px;
}
.fxhh .fxhh_ctt .fh_c_show .fh_c_under li .botTit {
  margin-top: 10px;
}
.fxhh .fxhh_ctt .fhc_box {
  display: none;
  width: 960px;
  height: 4px;
  background-color: #f3f3f3;
  margin: 0 auto;
  position: absolute;
  top: 250px;
  left: 210px;
}
.fxhh .fxhh_ctt .fhc_drop {
  width: 99px;
  height: 9px;
  border-radius: 4px;
  background-color: #d8d8d8;
  position: absolute;
  top: -3px;
}

总结

以上所述是小编给大家介绍的JS实现可用滑块滑动的缓动图代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

Javascript 相关文章推荐
JavaScript打开word文档的实现代码(c#)
Apr 16 Javascript
jquery实现点击TreeView文本父节点展开/折叠子节点
Jan 10 Javascript
javascript 回调函数详解
Nov 11 Javascript
微信小程序 progress组件详解及实例代码
Oct 25 Javascript
使用jQuery操作DOM的方法小结
Feb 27 Javascript
JavaScript调试之console.log调试的一个小技巧分享
Aug 07 Javascript
jQuery 防止相同的事件快速重复触发方法
Feb 08 jQuery
jQuery+Cookie实现切换皮肤功能【附源码下载】
Mar 25 jQuery
vue项目中跳转到外部链接的实例讲解
Sep 20 Javascript
详解jQuery-each()方法
Mar 13 jQuery
详细讲解如何创建, 发布自己的 Vue UI 组件库
May 29 Javascript
H5+css3+js搭建带验证码的登录页面
Oct 11 Javascript
vue动态子组件的两种实现方式
Sep 01 #Javascript
Vue CLI项目 axios模块前后端交互的使用(类似ajax提交)
Sep 01 #Javascript
简单分析js中的this的原理
Aug 31 #Javascript
微信小程序image图片加载完成监听
Aug 31 #Javascript
JS实现使用POST方式发送请求
Aug 30 #Javascript
node.js处理前端提交的GET请求
Aug 30 #Javascript
Node.js系列之连接DB的方法(3)
Aug 30 #Javascript
You might like
smarty内置函数foreach用法实例
2015/01/22 PHP
浅谈PHP的数据库接口和技术
2016/12/09 PHP
用PHP的socket实现客户端到服务端的通信实例详解
2017/02/04 PHP
php微信开发之音乐回复功能
2018/06/14 PHP
Laravel框架实现多个视图共享相同数据的方法详解
2019/07/09 PHP
基于php伪静态的实现方法解析
2020/07/31 PHP
Javascript公共脚本库系列(一): 弹出层脚本
2011/02/24 Javascript
JQuery魔力之$(&quot;tagName&quot;)与selector
2012/03/05 Javascript
jquery禁用右键示例
2014/04/28 Javascript
jQuery分组选择器用法实例
2014/12/23 Javascript
Windows系统下Node.js的简单入门教程
2015/06/23 Javascript
学习JavaScript设计模式之责任链模式
2016/01/18 Javascript
手机端实现Bootstrap简单图片轮播效果
2016/10/13 Javascript
JS访问DOM节点方法详解
2016/11/29 Javascript
jQuery实现的分页功能示例
2017/01/22 Javascript
jquery引入外部CDN 加载失败则引入本地jq库
2018/05/23 jQuery
vue slots 组件的组合/分发实例
2018/09/06 Javascript
微信小程序实现打卡日历功能
2020/09/21 Javascript
基于node.js实现爬虫的讲解
2019/02/18 Javascript
移动端自适应flexible.js的使用方法(不用三大框架,仅写一个单html页面使用)推荐
2019/04/02 Javascript
vue draggable resizable 实现可拖拽缩放的组件功能
2019/07/15 Javascript
layui表格 返回的数据状态异常的解决方法
2019/09/10 Javascript
初学vue出现空格警告的原因及其解决方案
2019/10/31 Javascript
[01:12:08]LGD vs OG 2019国际邀请赛淘汰赛 胜者组 BO3 第一场 8.24
2019/09/10 DOTA
python查询mysql中文乱码问题
2014/11/09 Python
在Python中使用列表生成式的教程
2015/04/27 Python
python下paramiko模块实现ssh连接登录Linux服务器
2015/06/03 Python
对web.py设置favicon.ico的方法详解
2018/12/04 Python
python实现微信定时每天和女友发送消息
2019/04/29 Python
python 使用raw socket进行TCP SYN扫描实例
2020/05/05 Python
HTML5之SVG 2D入门8—文档结构及相关元素总结
2013/01/30 HTML / CSS
施华洛世奇韩国官网:SWAROVSKI韩国
2018/06/05 全球购物
骨干教师考核方案
2014/05/09 职场文书
质量标语大全
2014/06/12 职场文书
行政助理岗位职责
2015/02/10 职场文书
欠款起诉书范文
2015/05/19 职场文书