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

jquery實(shí)現(xiàn)拖拽小方塊效果

 更新時(shí)間:2020年12月10日 14:21:44   作者:賺錢養(yǎng)個(gè)快樂(lè)小智障  
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)拖拽小方塊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

今天來(lái)講一個(gè)很常用的拖拽功能,主要是利用一點(diǎn)css,js和jquery,很簡(jiǎn)單但同時(shí)也很重要,掌握好才是最關(guān)鍵的!

下面來(lái)看下效果圖:

這里可以看到,在鼠標(biāo)hover的時(shí)候會(huì)有一個(gè)透明度的變化和一個(gè)盒子陰影!并且在右下角會(huì)實(shí)時(shí)的顯示出小方塊移動(dòng)時(shí)橫縱坐標(biāo)的變化!

可以看到有一個(gè)盒子陰影

在鼠標(biāo)單擊按住的時(shí)候會(huì)變紅,然后可以拖動(dòng)小塊隨意移動(dòng)

我們?cè)诳创a之前可以先了解下jquery中的基本知識(shí)(選擇器,css控制器什么的)和offset()clientX,clientY。思路很簡(jiǎn)單,就是分別監(jiān)聽(tīng)鼠標(biāo)的 mousedown,mouseup,mousemove這三個(gè)事件,通過(guò)判斷div移動(dòng)之前的坐標(biāo),div移動(dòng)之后的坐標(biāo)(offset獲得)和鼠標(biāo)移動(dòng)前后的坐標(biāo)(clientX和clientY獲得)判斷div在body里的具體坐標(biāo)位置,然后設(shè)置div在body里左邊和上邊的距離(left,top)。代碼思路在代碼里很詳細(xì)!希望大家好好理解!

附上代碼:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>拖動(dòng)圖片</title>
  <style>
    html {
      height: 100%;
    }
    
    body {
      margin: 0px;
      padding: 0rem;
      border: 0rem;
      height: 100%;
      width: 100%;
      position: relative;
      /* 取消默認(rèn)的輸入事件,不然會(huì)一直出現(xiàn)‘I'一樣的光標(biāo) */
      -webkit-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;
    }
    
    #drag {
      height: 200px;
      width: 200px;
      background: teal;
      border: none;
      border-radius: 1rem;
      /* 一定要是absolute */
      position: absolute;
      opacity: 0.8;
    }
    
    #drag:hover {
      box-shadow: 0 8px 12px 0 rgba(16, 49, 231, 0.4);
      opacity: 1;
      cursor: pointer;
    }
    
    input {
      width: 12rem;
      height: 2rem;
      font-size: 1.5rem;
      border: 2px solid #aaa;
    }
    
    #input1 {
      display: block;
      position: absolute;
      bottom: 1.2rem;
      right: 1rem;
    }
    
    #input2 {
      display: block;
      position: absolute;
      bottom: 1.2rem;
      right: 16rem;
    }
  </style>
</head>

<body>

  <div id="drag">

  </div>
  <input type="text" id="input1" name="y" placeholder="y軸的坐標(biāo)為">
  <input type="text" id="input2" name="x" placeholder="x軸的坐標(biāo)為">

  <!-- 引入內(nèi)部jquery,大家使用可以引入CDN -->
  <script src="jquery-3.5.1.min.js"></script>
  <script>
    var client_x = 0;
    var client_y = 0;
    var offset_x = 0;
    var offset_y = 0;
    var moving = false;
    // 利用jquery獲得div這個(gè)元素
    var drag = $('#drag');
    // 添加監(jiān)聽(tīng)事件
    drag.on({
      // 鼠標(biāo)抬起事件
      mouseup: function(e) {
        moving = false;
        // 為div添加一個(gè)css樣式
        $("#drag").css("background", 'teal');
      },
      // 鼠標(biāo)按下事件
      mousedown: function(e) {
        moving = true;
        // this代表的是 div
        var offset = $(this).offset();
        // offset() 方法設(shè)置或返回被選元素相對(duì)于文檔的偏移坐標(biāo)
        offset_x = offset.left;
        offset_y = offset.top;
        // clientX 事件屬性返回當(dāng)事件被觸發(fā)時(shí)鼠標(biāo)指針相對(duì)于瀏覽器頁(yè)面的水平坐標(biāo)
        client_x = e.clientX;
        // clientX 事件屬性返回當(dāng)事件被觸發(fā)時(shí)鼠標(biāo)指針相對(duì)于瀏覽器頁(yè)面的垂直坐標(biāo)
        client_y = e.clientY;
        drag.css("background", 'rgb(179, 43, 19)');
      },
      // 鼠標(biāo)移動(dòng)事件
      mousemove: function(e) {
        if (moving) {
          // 為div添加一個(gè)css樣式
          drag.css({
            left: offset_x + (e.clientX - client_x),
            top: offset_y + (e.clientY - client_y)
          });
          // 設(shè)置并顯示input標(biāo)簽內(nèi)x,y軸的坐標(biāo)
          $(':input[name="x"]').val(offset_x + (e.clientX - client_x));
          $(':input[name="y"]').val(offset_y + (e.clientY - client_y));
          drag.css("cursor", "pointer");
        }
      }
    });
  </script>
</body>

</html>

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

相關(guān)文章

最新評(píng)論