欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)容器模塊左右拖動(dòng)效果

 更新時(shí)間:2020年01月14日 08:35:35   作者:李狗蛋96  
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)容器模塊左右拖動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了JS實(shí)現(xiàn)容器模塊左右拖動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)際效果是 鼠標(biāo)點(diǎn)擊上方顏色模塊,左右拖動(dòng),根據(jù)拖動(dòng)的距離來顯示模塊
一共有7個(gè)顏色塊

代碼如下

CSS

#box {
  /*margin: 0 auto;*/
  width: 1750px;
  border: 1px solid black;
  display: block;
  position: relative;
  left: 0;
  }

  #box>div {
  width: 250px;
  height: 50px;
  display: inline-block;
  text-align: center;
  float: left;
  }

  h1 {
  padding: 0;
  margin: 0;
  }

Html

<div style="width: 1000px;overflow: hidden;margin: auto;position: relative;">
  <div id="box">
  <div style="background: lemonchiffon;">
   <h1>one</h1></div>
  <div style="background: lightblue;">
   <h1>tow</h1></div>
  <div style="background: gold;">
   <h1>three</h1></div>
  <div style="background: blue;">
   <h1>four</h1></div>
  <div style="background: orange;">
   <h1>five</h1></div>
  <div style="background: aqua;">
   <h1>six</h1></div>
  <div style="background: brown;">
   <h1>seveen</h1></div>
  </div>
  <h1>拖動(dòng)上面的顏色模塊</h1>
 <p>實(shí)際原理還是輪播圖</p>
</div>
<div style="width: 1000px;overflow: hidden;margin: auto;position: relative;">

注意:最大容器的DIV中寬度設(shè)置為1000px 表示展示的內(nèi)容為1000,超出的被隱藏,根據(jù)自己想展示的模塊來設(shè)計(jì)id為‘box'的DIV則為超大寬度的容器,里面模塊以浮動(dòng)形式布局,原理跟輪播圖一樣'**。

JS

<script src="jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
 <script type="text/javascript">
 let box = document.getElementById('box')
 $box = $('#box')
 let mouseState = false; //鼠標(biāo)默認(rèn)狀態(tài)
 let startX = 0;
 let startY = 0;
 let moveDirection = 0 //鼠標(biāo)拖動(dòng)距離
 $boxLeft = parseInt($box.css('left'))//表示內(nèi)容塊被偏移的值
 //鼠標(biāo)按下事件
 box.addEventListener('mousedown', function(e) {
  //更改鼠標(biāo)狀態(tài)
  //參數(shù)e為鼠標(biāo)
  mouseState = true
  //獲取鼠標(biāo)坐標(biāo)
  startX = e.clientX
  startY = e.clientY
  //鼠標(biāo)拖動(dòng)初始化
  moveDirection = 0
  $boxLeft = parseInt($box.css('left'))
 })
 //鼠標(biāo)移動(dòng)狀態(tài)
 box.addEventListener('mousemove', function(e) {
  //判斷鼠標(biāo)是不是被按下中移動(dòng)
  if(mouseState) {
  //判斷是向左還是向右拖動(dòng)鼠標(biāo)
  moveDirection = e.clientX - startX
  //向右移動(dòng)
  box.style.left = $boxLeft + moveDirection + 'px'
  }
 })
 //鼠標(biāo)彈起事件
 box.addEventListener('mouseup', function(e) {
  mouseState = false
  if(moveDirection > 0) {
  //$boxLeft<0表示已經(jīng)看到最左的模塊,不能在向右拖拉
  $boxLeft = parseInt($box.css('left'))
  //取余,比如拖動(dòng)了52px,實(shí)際內(nèi)容塊就250px,偏移余下的px就能看的完整的內(nèi)容塊
  $num = $boxLeft % 250
  if($boxLeft < 0) {
   $box.animate({
   left: $boxLeft - $num + 'px'
   }, 500, function() {
   console.log(parseInt($box.css('left')))
   })
  } else {
  //向右偏移,如果$boxLeft大于等于0的話 那就是還是第一個(gè)顏色模塊,不允許被偏移
   $box.animate({
   left: 0 + 'px'
   }, 500, function() {})
  }
  } else if(moveDirection < 0) {
  //$boxLeft>-750表示已經(jīng)看到最右邊的模塊,不能在向左拖拉
  $boxLeft = parseInt($box.css('left'))
  $num = 250 + $boxLeft % 250
  if($boxLeft > -750) {
   $box.animate({
   left: $boxLeft - $num + 'px'
   }, 500, function() {
   console.log(parseInt($box.css('left')))
   })
  }else{
  //向左偏移,如果$boxLeft小于等于-750px的話 那就是最后一個(gè)顏色模塊,不允許被偏移
   $box.animate({
   left: -750 + 'px'
   }, 500, function() {})
  }
  }
 })
</script>

OK了。應(yīng)該能看的懂了

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論