js 给div 添加选区,类似windows桌面选区一样,兼容ie6、7、8、9、10、Firefox、Google、所有浏览器
2013-06-16 13:39
561 查看
先来一张效果图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201908/19/124c9e29fbce6c121fc0f5ebd58e2b08)
兼容:
IE6 反应有点迟钝,效果没有火狐好。
主要的效果就是,模仿windows桌面的那个选区。可以用选框选区图标。
由于js代码太多,只贴一段核心的代码。
js核心代码:
//选区
function select(){
//为工作区创建一个选区
var _down=false;
var pos={x:0,y:0};
var move={x:0,y:0};
$(".working").mousedown(function(event) {
//如果是右键
if(event.button==2)return;
//不是点击的桌面 返回
if(!$(event.target).is(".icons"))return;
_down=true;
pos.x=event.clientX;
pos.y=event.clientY;
$(".select").css("left",pos.x);
$(".select").css("top",pos.y);
$(".select").show();
});
$(".working").mousemove(function(event) {
if(!_down)return;
move.x=event.clientX;
move.y=event.clientY;
//减去2 兼容ie
var width=move.x-pos.x-2;
var height=move.y-pos.y-2;
var select=$(".select");
//选区向左
if(width>=0&&height>=0){
select.css("width",width);
select.css("height",height);
}else{
//选区向右延伸
if(width<0){
//求绝对值
var w=Math.abs(width);
select.css("width",w);
select.css("left",move.x);
if(height>=0){
select.css("height",height);
}
}
if (height<=0) {
var h=Math.abs(height);
select.css("height",h);
select.css("top",move.y);
if(width>=0){
select.css("width",width);
}
}
}
});
$(".working").mouseup(function() {
_down=false;
$(".select").css("width",0);
$(".select").css("height",0);
$(".select").hide();
});
}
上面的效果当然还需要配合css 来做。完整的Dome过一段时间发。
兼容:
IE6 反应有点迟钝,效果没有火狐好。
主要的效果就是,模仿windows桌面的那个选区。可以用选框选区图标。
由于js代码太多,只贴一段核心的代码。
js核心代码:
//选区
function select(){
//为工作区创建一个选区
var _down=false;
var pos={x:0,y:0};
var move={x:0,y:0};
$(".working").mousedown(function(event) {
//如果是右键
if(event.button==2)return;
//不是点击的桌面 返回
if(!$(event.target).is(".icons"))return;
_down=true;
pos.x=event.clientX;
pos.y=event.clientY;
$(".select").css("left",pos.x);
$(".select").css("top",pos.y);
$(".select").show();
});
$(".working").mousemove(function(event) {
if(!_down)return;
move.x=event.clientX;
move.y=event.clientY;
//减去2 兼容ie
var width=move.x-pos.x-2;
var height=move.y-pos.y-2;
var select=$(".select");
//选区向左
if(width>=0&&height>=0){
select.css("width",width);
select.css("height",height);
}else{
//选区向右延伸
if(width<0){
//求绝对值
var w=Math.abs(width);
select.css("width",w);
select.css("left",move.x);
if(height>=0){
select.css("height",height);
}
}
if (height<=0) {
var h=Math.abs(height);
select.css("height",h);
select.css("top",move.y);
if(width>=0){
select.css("width",width);
}
}
}
});
$(".working").mouseup(function() {
_down=false;
$(".select").css("width",0);
$(".select").css("height",0);
$(".select").hide();
});
}
上面的效果当然还需要配合css 来做。完整的Dome过一段时间发。
相关文章推荐
- 网页调用JS音频播放器兼容所有浏览器,包括IE6,7,8,9,FF,GOOGLE,OPERA,360等
- div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法
- div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法
- div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法
- div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法
- div+css 兼容 ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法
- Div+CSS浏览器兼容问题( FireFox IE6 IE7 IE8 IE9)
- JS 预览上传图片,兼容IE、firefox等所有浏览器
- 兼容IE6,IE7,IE8,Firefox,Chrome,DIV+CSS垂直居中终于失败,最后三分钟用写了几行JS解决,经简单测试,还行。
- div+css+js实现无缝滚动类似marquee无缝滚动兼容firefox
- div+css兼容 ie6_ie7_ie8_ie9_ie10和FireFox_Chrome等浏览器方法
- js可选择时分秒的日历时间控件代码下载,多浏览器IE8,9,10、谷歌chrome,火狐firefox等都是兼容的
- div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法
- div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法(原创理论)
- 如何让div+css兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器
- div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法
- div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法
- js 打印DIV内容 print div 兼容IE FF Google等主流浏览器
- div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法[ZT]
- div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法