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

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

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实现可用滑块滑动的缓动图代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

(0)

相关推荐

  • Javascript实现滑块滑动改变值的实现代码

    最近做了一个关于税务的功能,值得一说的是本页面的滑块实现.大家都知道现实中的程序员大部分都是对于页面和美工不是很熟悉. 本人也是,但是本人比较喜欢自己动手来实现.废话不多说.上图: 实现结果: 部分js代码: 复制代码 代码如下: window.onload = function () { var oWin = document.getElementById("win"); var bDrag = false; var disX = disY = 0; oWin.onmousedown

  • Angular.js通过自定义指令directive实现滑块滑动效果

    前言 最近因为工作需要让我一个效果实现在页面某一部分内滑块随着滚动条上下滑动,说明一下我们项目使用技术angularJs.大家都知道,使用jquery很好实现. 那么angular如何实现呢,我用的是自定义指令(directive). 方法如下 1.下面是我html部分代码,detail-scroll是我自定义的标签 ............... <div id="time" style="position: relative;"> <div n

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

    尝试模仿京东的"发现好货"模块的可用滑块滑动的缓动图 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) { f

  • JS实现可用滑块滑动的缓动图

    尝试模仿京东的"发现好货"模块的可用滑块滑动的缓动图 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 <

  • js实现简洁的滑动门菜单(选项卡)效果代码

    本文实例讲述了js实现简洁的滑动门菜单.分享给大家供大家参考.具体如下: 一个简洁实用的网页选项卡菜单,在同一个页面中实现两个选项卡,第一个是滑动门,从布局上来说基本是相似的,第二个是选项卡,这里主要使用了Js自定义函数:tabMenu,函数功能:实现tab菜单,参数说明:tabMenu(tabBox,navClass); 参数一:tabBox(tab容器id) 参数二:navClass(当前标签样式class) 备注:依赖指定html结构. 运行效果截图如下: 在线演示地址如下: http:/

  • JavaScript Tween算法及缓动效果第1/2页

    我这里要教大家的是怎么利用flash的Tween类的算法,来做js的Tween算法,并利用它做一些简单的缓动效果. Tween Tween类型: Linear Quadratic Cubic Quartic Quintic Sinusoidal Exponential Circular Elastic Back Bounce ease类型: easeIn easeOut easeInOut /* 算法来源:http://www.robertpenner.com/easing/ */ var Tw

  • JavaScript 详解缓动动画的封装与使用

    实现过程分析 (1)如何重复调用? 答:封装一个函数,用一次调用一次 代码分析: function animate(obj, target, callback) { //详细实现步骤 }; animate :(动画函数) obj(动画对象):给谁添加动画效果 target(目标值):移动到什么距离 callback(回调函数):同时要执行什么功能 (2)如何实现缓动效果?(缓动动画核心算法) 答:移动距离 =(目标值 - 现在盒子位置)/ 10,移动距离会慢慢变小,直至停下,就实现了缓动原理 代

  • canvas+gif.js打造自己的数字雨头像的示例代码

    前天 是1024程序员节,不知道各位看官过的怎么样.既然是过节,就要有个过节的样子,比方说,换个头像

  • 利用Python轻松实现视频转GIF动图

    目录 前言 1. 准备工作 2. 初探 3. 截取区域转动图 4. 固定区域转动图 5. 添加自定义文本 前言 不知道大家是不是有过类似的经历,在看视频的时候觉得某段非常有意思想弄成动图,但是无从下手! 或可以在网上找一些在线工具但是多多少少需要付费或者带有水印之类的,那么!? 对,今天我们就来学习用Python搞定这一需求吧! 动图效果 1. 准备工作 需要准备用于生成gif的视频文件,我这里用的是上次<用Python制作一个B站视频下载小工具>里案例中的视频.另外,就是需要用到moviep

  • JS+HTML5手机开发之滚动和惯性缓动实现方法分析

    本文实例讲述了JS+HTML5手机开发之滚动和惯性缓动实现方法.分享给大家供大家参考,具体如下: 1. 滚动 以下是三种实现方式: 1) 利用原生的css属性 overflow: scroll div id= parent style = overflow:scroll; divid='content'内容区域/div /div Notice: 在android 有bug, 滚动完后会回退到最顶端的内容区域,解决办法是使用后两种方式实现 2)js 编程实现 思路:对比手指在屏幕上移动前后位置变化

  • JS轮播图中缓动函数的封装

    轮播图的根本其实就是缓动函数的封装,如果说轮播图是一辆跑动的汽车,那么缓动函数就是它的发动机,今天本文章就带大家由简入繁,封装属于自己的缓动函数~~ 我们从需求的角度开始,首先给出一个简单需求: 1.我想让页面中的一个盒子从开始的位置匀速向右运动到200px的地方,该怎么实现? 分析: 1)我们需要知道盒子在哪个地方,这个可以通过offsetLeft属性去获取: 2)要让盒子匀速运动,对于js肯定需要setInterval了: 3)要让盒子向右边跑起来?那就是需要不停改变盒子与左边起始点的距离,

随机推荐