您的位置:首页 > 教程笔记 > 前端笔记

jquery实现的滑动轴效果代码实例

2023-12-04 13:40:25 前端笔记 125

本章节分享一段代码实例,它实现了使用鼠标鼠标拖动元素滑动效果。

也就是滑动轴效果,并且能够选取数字,代码实例:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="/jquery/1.9.0/jquery.js"></script>
<script>
$(document).ready(function(){
  var $dom=$(document);
  $dom.on('mousedown','#d',function(argument){
    $(this).data('mouse','down');
  })
  $dom.on('mouseup',function(){
    $('#d').data('mouse','up');
  });
  $dom.on('mousemove','#d',function(event){
    if($(this).data('mouse') == 'down'){
      var m_x = event.clientX;
      var d_b = $(this).find('.d_b');
      m_x = m_x < 8 ? 8 : m_x;
      m_x = m_x > 208 ? 208: m_x;
      d_b.css('left',m_x-13);
      var max = $(this).attr('max');
      $(this).attr('value', Math.floor((m_x-8)/200 * max))
      $('#text').text($(this).attr('value'))
    }
  });
});
</script>
</head>
<body>
<div id="d" max="100" min="0" value="10">
  <div id="antzone"></div>
  <b class="d_b"></b>
</div>
<b id="text"></b>
</body>
</html>

相关推荐