jQuery實(shí)現(xiàn)鼠標(biāo)拖動(dòng)圖片功能
本例利用jQuery實(shí)現(xiàn)一個(gè)鼠標(biāo)托動(dòng)圖片的功能。
首先設(shè)一個(gè)wrapper,wrapper內(nèi)的坐標(biāo)即圖片移動(dòng)的坐標(biāo)
#wrapper{
width: 1000px;
height:1000px;
position:relative;
}
設(shè)置圖片div,這個(gè)div即要拖動(dòng)的div
#div1{
position: absolute;
left:0px;
top:0px;
width: 300px;
height: 200px;
background: url("d:/Pictures/Earth.jpg");
background-size:contain;
}
上面設(shè)置了wrapper的定位為relative,div1的定位為absolute。
接下來設(shè)計(jì)拖動(dòng)的算法:
思路如下:
1.鼠標(biāo)點(diǎn)下時(shí)讓div跟隨鼠標(biāo)移動(dòng)
2.鼠標(biāo)松開時(shí)停止跟隨
首先需要一個(gè)函數(shù),他會將該div的坐標(biāo)改變?yōu)楫?dāng)前鼠標(biāo)的位置:
首先需要定義幾個(gè)變量,保存當(dāng)前鼠標(biāo)的坐標(biāo)以及圖片的坐標(biāo)
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添加一個(gè)事件監(jiān)聽器,鼠標(biāo)在wrapper中移動(dòng)時(shí),修改變量mousex,mousey的值
$("#wrapper").mousemove(function(e){
mouseX = e.clientX;
mouseY = e.clientY;
});
編寫follow函數(shù),并用計(jì)時(shí)器調(diào)用它
$("#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>
最終效果:

到此這篇關(guān)于jQuery實(shí)現(xiàn)鼠標(biāo)拖動(dòng)圖片功能的文章就介紹到這了,更多相關(guān)jQuery鼠標(biāo)拖動(dòng)圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于JQuery的asp.net樹實(shí)現(xiàn)代碼
在網(wǎng)上找了tree,想直接拿來用,誰知道竟然沒有找到基于asp.net的tree,索性自己便把jquery的tree拿來研究了下,然后結(jié)合者asp.net,做了一個(gè)遞歸樹.2010-11-11
jQuery使用eraser.js插件實(shí)現(xiàn)擦除、刮刮卡效果的方法【附eraser.js下載】
這篇文章主要介紹了jQuery使用eraser.js插件實(shí)現(xiàn)擦除、刮刮卡效果的方法,結(jié)合實(shí)例形式分析了jQuery.eraser.js插件的功能、使用方法與相關(guān)注意事項(xiàng),并附帶eraser.js插件源碼供讀者下載使用,需要的朋友可以參考下2017-04-04
淺析JQuery獲取和設(shè)置Select選項(xiàng)的常用方法總結(jié)
本篇文章是對JQuery獲取和設(shè)置Select選項(xiàng)的一些常用方法進(jìn)行了匯總介紹,有需要的朋友可以參考一下2013-07-07
解決jquery validate 驗(yàn)證不通過后驗(yàn)證正確的信息仍殘留在label上的方法
在本篇文章里小編給大家整理了關(guān)于解決jquery validate 驗(yàn)證不通過后驗(yàn)證正確的信息仍殘留在label上的方法,有需要的朋友們可以學(xué)習(xí)下。2019-08-08

