本文實例分析了jQuery實現div拖拽效果。分享給大家供大家參考,具體如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><title> New Document </title><script type="text/javascript" src="jquery-1.7.2.min.js"></script><style type="text/css">/*模塊拖拽*/.drag{position:absolute;width:100px;height:100px;border:1px solid #ddd;background:#FBF2BD;text-align:center;padding:5px;top:100px;left:20px;cursor:move;}</style><script type="text/javascript">// 模塊拖拽$(function(){var _move=false;//移動標記var _x,_y;//鼠標離控件左上角的相對位置 $(".drag").click(function(){ //alert("click");//點擊(松開后觸發) }).mousedown(function(e){ _move=true; _x=e.pageX-parseInt($(".drag").css("left")); _y=e.pageY-parseInt($(".drag").css("top")); $(".drag").fadeTo(20, 0.5);//點擊后開始拖動并透明顯示 }); $(document).mousemove(function(e){ if(_move){ var x=e.pageX-_x;//移動時根據鼠標位置計算控件左上角的絕對位置 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>
$("#question_pic").bind("click",function(event){ event.stopPropagation(); //防止冒泡事件響應 $("#chat_question").hide("slow");});
$("#chatLineHolder").scrollTop(10000);//保持最下一行,不用滾
更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery拖拽特效與技巧總結》、《jQuery常見經典特效匯總》、《jQuery擴展技巧總結》、《jquery選擇器用法總結》及《jquery常用操作技巧匯總》
希望本文所述對大家jQuery程序設計有所幫助。
新聞熱點
疑難解答