jQuery實現(xiàn)鼠標拖動圖片功能
本例利用jQuery實現(xiàn)一個鼠標托動圖片的功能。
首先設一個wrapper,wrapper內的坐標即圖片移動的坐標
#wrapper{
width: 1000px;
height:1000px;
position:relative;
}
設置圖片div,這個div即要拖動的div
#div1{
position: absolute;
left:0px;
top:0px;
width: 300px;
height: 200px;
background: url("d:/Pictures/Earth.jpg");
background-size:contain;
}
上面設置了wrapper的定位為relative,div1的定位為absolute。
接下來設計拖動的算法:
思路如下:
1.鼠標點下時讓div跟隨鼠標移動
2.鼠標松開時停止跟隨
首先需要一個函數(shù),他會將該div的坐標改變?yōu)楫斍笆髽说奈恢茫?/p>
首先需要定義幾個變量,保存當前鼠標的坐標以及圖片的坐標
var timer;
var mouseX=0;
var mouseY=0;
var pic_width = parseInt($("#div1").css("width"));
var pic_height = parseInt($("#div1").css("height"));
那么現(xiàn)在就需要為wrapper添加一個事件監(jiān)聽器,鼠標在wrapper中移動時,修改變量mousex,mousey的值
$("#wrapper").mousemove(function(e){
mouseX = e.clientX;
mouseY = e.clientY;
});
編寫follow函數(shù),并用計時器調用它
$("#div1").mousedown(function(){
timer=setInterval(follow,10);
});
$("#div1").mouseup(function(){
clearInterval(timer);
});
var follow = function(){
$("#div1").css("left",mouseX-pic_width/2);
$("#div1").css("top",mouseY-pic_height/2);
};
完整代碼如下所示:
<!doctype html>
<html>
<head>
<script type = "text/javascript" src = "jquery.js"></script>
<style type = "text/css">
#wrapper{
width: 1000px;
height:1000px;
position: relative;
background: linear-gradient(lightblue,white);
font-size: 40px;
}
#div1{
position: absolute;
left:0px;
top:0px;
width: 300px;
height: 200px;
background: url("d:/Pictures/Earth.jpg");
background-size:contain;
}
</style>
</head>
<body>
<div id = "wrapper">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Impedit numquam accusamus explicabo praesentium laudantium et accusantium, ab ipsum, excepturi necessitatibus quos iste ad qui deleniti sed debitis reiciendis quam nisi.
<div id = "div1">
</div>
</div>
<script>
var timer;
var mouseX=0;
var mouseY=0;
var pic_width = parseInt($("#div1").css("width"));
var pic_height = parseInt($("#div1").css("height"));
$("#wrapper").mousemove(function(e){
mouseX = e.clientX;
mouseY = e.clientY;
});
$("#div1").mousedown(function(){
timer=setInterval(follow,10);
});
$("#div1").mouseup(function(){
clearInterval(timer);
});
var follow = function(){
$("#div1").css("left",mouseX-pic_width/2);
$("#div1").css("top",mouseY-pic_height/2);
};
</script>
</body>
</html>
最終效果:

到此這篇關于jQuery實現(xiàn)鼠標拖動圖片功能的文章就介紹到這了,更多相關jQuery鼠標拖動圖片內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
getJSON調用后臺json數(shù)據(jù)時函數(shù)被調用兩次的原因猜想
近期在做前端開發(fā)時候使用到getJSON調用后臺json數(shù)據(jù),發(fā)現(xiàn)后臺的函數(shù)被調用兩次,函數(shù)名稱為getMessages,下面是本人的一些猜想,感興趣的朋友可以參考下2013-09-09
jQuery Validate 驗證,校驗規(guī)則寫在控件中的具體實例
本篇文章主要是對jQuery Validate 驗證,校驗規(guī)則寫在控件中的具體實例進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02
jQuery實現(xiàn)將頁面上HTML標簽換成另外標簽的方法
這篇文章主要介紹了jQuery實現(xiàn)將頁面上HTML標簽換成另外標簽的方法,實例說明了兩種實現(xiàn)html頁面元素替換的技巧,需要的朋友可以參考下2015-06-06
使用jQuery加載html頁面到指定的div實現(xiàn)方法
下面小編就為大家?guī)硪黄褂胘Query加載html頁面到指定的div實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07
jQuery中bind,live,delegate與one方法的用法及區(qū)別解析
本篇文章主要是對jQuery中bind,live,delegate與one方法的用法及區(qū)別進行了詳細的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12

