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

腳本div實(shí)現(xiàn)拖放功能(兩種)

 更新時(shí)間:2017年02月13日 16:58:00   作者:sumer7310  
本文介紹了腳本div實(shí)現(xiàn)拖放功能的兩種方法:1.原生拖放實(shí)現(xiàn);2.jQuery UI draggable實(shí)現(xiàn)拖放。具有很好的參考價(jià)值,下面跟著小編一起來看下吧

網(wǎng)頁上有很多拖曳的操作,比如拖動(dòng)樹狀列表,可拖曳的圖片等。

1.原生拖放實(shí)現(xiàn)

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>jQuery UI Autocomplete - Default functionality</title>
 <link rel="stylesheet"  rel="external nofollow" >
 <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
 <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
 <style>
 .drag{
 width: 200px;
 height: 200px;
 background-color: red;
 position: absolute;
 left:0;
 top:0;
 }
 </style>
 <script>
 $(function() {
 var _move = false;//判斷目標(biāo)對(duì)象書否處于移動(dòng)狀態(tài)
 var _x, _y;//鼠標(biāo)離控件左上角的相對(duì)x.y坐標(biāo)
 $('.drag').click(function(event) {
 }).mousedown(function(e) {//當(dāng)按下鼠標(biāo)左鍵時(shí)
 _move = true;//標(biāo)記移動(dòng)為true,開始移動(dòng)
 _x = e.pageX - parseInt($('.drag').css('left'));//得到左上角的x的位置
 _y = e.pageY - parseInt($('.drag').css('top'));//得到左上角的y的位置
 $('.drag').fadeTo('20', 0.5);//單擊后開始拖動(dòng)
 
 });
 
 $(document).mousemove(function(e) {//監(jiān)聽鼠標(biāo)移動(dòng)
 if(_move) {
 var x = e.pageX - _x;//計(jì)算移動(dòng)的距離
 var y = e.pageY - _y;
 $('.drag').css({top:y, left:x});
 }
 }).mouseup(function() {
 _move = false;
 $('.drag').fadeTo('fast', 1);
 });
 });
 </script>
</head>
<body>
 <div class="drag"></div>
</body>
</html>

2 jQuery UI draggable實(shí)現(xiàn)拖放

自行實(shí)現(xiàn)拖曳方法比較負(fù)責(zé),jQuery UI提供了可拖曳的事件,允許用戶非常簡(jiǎn)單的為一個(gè)div添加拖曳效果。

jQuery UI主要通過draggable事件來實(shí)現(xiàn)拖曳功能。

 <script>
 $(document).ready(function(e) {
  $('.drag').draggable({cursor: 'move'});
  $('#enable').click(function(e) {
 $('.drag').draggable('enable');
  });
  $('#disable').click(function(event) {
 $('.drag').draggable('disable');
  });
  $('#deatroy').click(function(event) {
 $('.drag').draggable('destroy');
  });
 })
 </script>
</head>
<body>
 <button id="enable">enable</button>
 <button id="disable">disable</button>
 <button id="destroy">destroy</button>
 <div class="drag">
 <p>請(qǐng)拖動(dòng)我!</p> 
 </div>
</body>

關(guān)于draggable的API可以參考draggalbe API

draggable 實(shí)例

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

最新評(píng)論