jquery简单的拖动效果
发布时间:2016-02-20, 11:25:31 分类:HTML | 编辑 off 网址 | 辅助
正文 2340字数 281,681阅读
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>简单拖曵原理实例</title>
<style type="text/css">
#drag{width:400px;height:300px;background:url(http://upload.yxgz.cn/uploadfile/2009/0513/20090513052611873.jpg);cursor:move;position:absolute;top:100px;left:100px;border:solid 1px #ccc;}
h2{color:#fff;background: none repeat scroll 0 0 rgba(16, 90, 31, 0.7);color:#FFFFFF;height:40px;line-height:40px;margin:0;}
</style>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
/*--------------拖曳效果----------------
*原理:标记拖曳状态dragging ,坐标位置iX, iY
* mousedown:fn(){dragging = true, 记录起始坐标位置,设置鼠标捕获}
* mouseover:fn(){判断如果dragging = true, 则当前坐标位置 - 记录起始坐标位置,绝对定位的元素获得差值}
* mouseup:fn(){dragging = false, 释放鼠标捕获,防止冒泡}
*/
var dragging = false;
var iX, iY;
$("#drag").mousedown(function(e) {
dragging = true;
iX = e.clientX - this.offsetLeft;
iY = e.clientY - this.offsetTop;
this.setCapture && this.setCapture();
return false;
});
document.onmousemove = function(e) {
if (dragging) {
var e = e || window.event;
var oX = e.clientX - iX;
var oY = e.clientY - iY;
$("#drag").css({"left":oX + "px", "top":oY + "px"});
return false;
}
};
$(document).mouseup(function(e) {
dragging = false;
$("#drag")[0].ReleaseCapture && $("#drag")[0].ReleaseCapture();
e.cancelBubble = true;
})
})
</script>
</head>
<body>
<div id="drag">
<h2>来拖动我啊</h2>
</div>
</body>
</html>
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
有过 1 条评论 »
简单拖曵原理实例 <script> function bigimg(srcs,tt){ if(!srcs) var srcs='35574339ab3c813'; //页面层 layer.open({ title:'<i class="fi-flag" style="font-size: 18px;margin-right:10px;"></i>'+tt+'<span style="font-size: 12px;margin-left:10px;color:green;"><i class="fi-lightbulb"></i>滚动鼠标放大缩小 按住左键拖动</span>', move: false, type: 2, //scrollbar: false, skin: 'layui-layer-rim', //加上边框 area: ['100%', '100%'], //宽高 content: 'img.php?srcs='+srcs }); //immmms()(); }</script>
<!DOCTYPE html> <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>简单拖曵原理实例</title> <style type="text/css"> #drag{width:400px;height:300px;background:url(http://upload.yxgz.cn/uploadfile/2009/0513/20090513052611873.jpg);position:absolute;top:100px;left:100px;border:solid 1px #ccc;} h2{color:#fff;background: none repeat scroll 0 0 rgba(16, 90, 31, 0.7);color:#FFFFFF;height:40px;line-height:40px;margin:0;} </style> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.9.0/jquery.min.js"></script> <script type="text/javascript"> </script> </head> <body> <div id="drag"> <h2>来拖动我啊</h2> </div> </body> </html>
js鼠标滚动图片等比例缩放代码 图片鼠标滚轮放大效果 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>js鼠标滚动图片等比例缩放代码</title> <style type="text/css"> img{ position: absolute; left:400px; top: 100px; } </style> <script type="text/javascript" src="wheel.js"></script> <script type="text/javascript"> window.onload=function (){ //var oImg=document.getElementsByTagName("img")[0]; var oImg=document.getElementById('imgjjjjs'); fnWheel(oImg,function (down,oEvent){ var oldWidth=this.offsetWidth; var oldHeight=this.offsetHeight; var oldLeft=this.offsetLeft; var oldTop=this.offsetTop; var scaleX=(oEvent.clientX-oldLeft)/oldWidth;//比例 var scaleY=(oEvent.clientY-oldTop)/oldHeight; if (down){ this.style.width=this.offsetWidth*0.9+"px"; this.style.height=this.offsetHeight*0.9+"px"; } else{ this.style.width=this.offsetWidth*1.1+"px"; this.style.height=this.offsetHeight*1.1+"px"; } var newWidth=this.offsetWidth; var newHeight=this.offsetHeight; this.style.left=oldLeft-scaleX*(newWidth-oldWidth)+"px"; this.style.top=oldTop-scaleY*(newHeight-oldHeight)+"px"; }); } </script> </head> <body> <div><img src="http://li.img.pan.lizhenqiu.com/f07ac1c21c09a4b8d99446b1137560a7.jpg" alt="" width="300" id="imgjjjjs"></div> </body> </html>
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <script src="js/jquery.min.js"></script> <script type="text/javascript" src="wheel.js"></script> <style> *{padding:0px;margin:0px;} .classsss{padding-top:2.5%;text-align: center;/*background: url(5-130H2191322-52.gif);*/width: 100%;height: 100%;/*background-repeat: no-repeat;background-position: center 20%;*/} .classssooo{max-width:95%;padding-bottom:2.5%;} #drag{ cursor:move; /*position: absolute; top:0px; left:0px;*/ } </style> </head> <body> <div id="dragdrag" class="classsss"> <img id="drag" src="u/<?php echo $_GET['srcs'];?>.jpg" class="classssooo" /> </div> <script type="text/javascript"> niii=0; window.onload=function (){ //var oImg=document.getElementsByTagName("img")[0]; var oImg=document.getElementById('drag'); //alert(oImg.offsetWidth); /*niii=0; if(!niii){ niii=1; $('#drag').css({'top':'2.5%','left':'50%','marginLeft':'-'+(oImg.offsetWidth/2)+'px','position':'absolute'}); }*/ fnWheel(oImg,function (down,oEvent){ var oldWidth=this.offsetWidth; var oldHeight=this.offsetHeight; var oldLeft=this.offsetLeft; var oldTop=this.offsetTop; if(!niii){ $("#dragdrag").removeClass('classsss'); $("#drag").removeClass('classssooo'); $('#drag').css({'position':'absolute'}); //oldTop='2.5%'; } niii=1; var scaleX=(oEvent.clientX-oldLeft)/oldWidth;//比例 var scaleY=(oEvent.clientY-oldTop)/oldHeight; if (down){ //$("div").removeClass('classsss'); //$("img").removeClass('classssooo'); this.style.width=this.offsetWidth*0.9+"px"; this.style.height=this.offsetHeight*0.9+"px"; /*var newWidthsss=this.offsetWidth*0.9; var newHeightsss=this.offsetHeight*0.9;*/ } else{ //$("div").removeClass('classsss'); //$("img").removeClass('classssooo'); this.style.width=this.offsetWidth*1.1+"px"; this.style.height=this.offsetHeight*1.1+"px"; /*var newWidthsss=this.offsetWidth*1.1; var newHeightsss=this.offsetHeight*1.1;*/ } var newWidth=this.offsetWidth; var newHeight=this.offsetHeight; /*if(newWidthsss>document.getElementById('fdasfdasfdsa').offsetWidth){ $("div").removeClass('classsss'); $("img").removeClass('classssooo'); }*/ this.style.left=oldLeft-scaleX*(newWidth-oldWidth)+"px"; this.style.top=oldTop-scaleY*(newHeight-oldHeight)+"px"; }); } $(function(){ /*--------------拖曳效果---------------- *原理:标记拖曳状态dragging ,坐标位置iX, iY * mousedown:fn(){dragging = true, 记录起始坐标位置,设置鼠标捕获} * mouseover:fn(){判断如果dragging = true, 则当前坐标位置 - 记录起始坐标位置,绝对定位的元素获得差值} * mouseup:fn(){dragging = false, 释放鼠标捕获,防止冒泡} */ var dragging = false; var iX, iY; $("#drag").mousedown(function(e) { dragging = true; iX = e.clientX - this.offsetLeft; iY = e.clientY - this.offsetTop; this.setCapture && this.setCapture(); return false; }); document.onmousemove = function(e) { if (dragging) { var e = e || window.event; var oX = e.clientX - iX; var oY = e.clientY - iY; $("#drag").css({"left":oX + "px", "top":oY + "px"}); if(!niii){ $("#dragdrag").removeClass('classsss'); $("#drag").removeClass('classssooo'); $('#drag').css({'position':'absolute'}); //oldTop='2.5%'; } niii=1; return false; } }; $(document).mouseup(function(e) { dragging = false; $("#drag")[0].ReleaseCapture && $("#drag")[0].ReleaseCapture(); e.cancelBubble = true; }) }) </script> </body> </html>