<div class="drag"> <p class="title">标题(点击标题拖动)</p> </div> <div class="drag1"> <p class="title">标题</p> 点击我移动 </div>
(function($){
$.fn.drag=function(options){
//默认配置
var defaults = {
handler:false,
opacity:0.5
};
// 覆盖默认配置
var opts = $.extend(defaults, options);
this.each(function(){
//初始标记变量
var isMove=false,
//handler如果没有设置任何值,则默认为移动对象本身,否则为所设置的handler值
handler=opts.handler?$(this).find(opts.handler):$(this),
_this=$(this), //移动的对象
dx,dy;
$(document)
//移动鼠标,改变对象位置
.mousemove(function(event){
// console.log(isMove);
if(isMove){
//获得鼠标移动后位置
var eX=event.pageX,eY=event.pageY;
//更新对象坐标
_this.css({'left':eX-dx,'top':eY-dy});
}
})
//当放开鼠标,停止拖动
.mouseup(function(){
isMove=false;
_this.fadeTo('fast', 1);
//console.log(isMove);
});
handler
//当按下鼠标,设置标记变量isMouseDown为true
.mousedown(function(event){
//判断最后触发事件的对象是否是handler
if($(event.target).is(handler)){
isMove=true;
$(this).css('cursor','move');
//console.log(isMove);
_this.fadeTo('fast', opts.opacity);
//鼠标相对于移动对象的坐标
dx=event.pageX-parseInt(_this.css("left"));
dy=event.pageY-parseInt(_this.css("top"));
}
});
});
};
})(jQuery);
$(function(){
//拖动标题
$(".drag").drag({
handler:$('.title'),//操作拖动的对象,此对象必须是移动对象的子元素
opacity:0.7 //设置拖动时透明度
});
//拖动主体对象
$(".drag1").drag({
opacity:0.7
});
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有