var _selectedRegions = [];
function RegionSelect(selRegionProp){
this.regions =[];
var _regions = document.getElementsBySelector(selRegionProp["region"]);
if(_regions && _regions.length > 0){
var _self = this;
for(var i=0; i< _regions.length;i++){
_regions[i].onmousedown = function(){
var evt = window.event || arguments[0];
if(!evt.shiftKey && !evt.ctrlKey){
// 清空所有select样式
_self.clearSelections(_regions);
this.className += " "+_self.selectedClass;
// 清空selected数组,并加入当前select中的元素
_selectedRegions = [];
_selectedRegions.push(this);
}else{
if(this.className.indexOf(_self.selectedClass) == -1){
this.className += " "+_self.selectedClass;
_selectedRegions.push(this);
}else{
this.className = this.className.replaceAll(_self.selectedClass,"");
_selectedRegions.remove(this);
}
}
clearEventBubble(evt);
}
this.regions.push(_regions[i]);
}
}
this.selectedClass = selRegionProp["selectedClass"];
this.selectedRegion = [];
this.selectDiv = null;
this.startX = null;
this.startY = null;
}
RegionSelect.prototype.select = function(){
var _self = this;
addEvent("mousedown",function(){
var evt = window.event || arguments[0];
_self.onBeforeSelect(evt);
clearEventBubble(evt);
},document);
addEvent("mousemove",function(){
var evt = window.event || arguments[0];
_self.onSelect(evt);
clearEventBubble(evt);
},document);
addEvent("mouseup",function(){
_self.onEnd();
},document);
}
RegionSelect.prototype.onBeforeSelect = function(evt){
if(!document.getElementById("selContainer")){
this.selectDiv = document.createElement("div");
this.selectDiv.style.cssText = "position:absolute;width:0px;height:0px;font-size:0px;margin:0px;padding:0px;border:1px dashed #0099FF;background-color:#C3D5ED;z-index:1000;filter:alpha(opacity:60);opacity:0.6;display:none;";
this.selectDiv.id = "selContainer";
document.body.appendChild(this.selectDiv);
}else{
this.selectDiv = document.getElementById("selContainer");
}
this.startX = posXY(evt).x;
this.startY = posXY(evt).y;
this.isSelect = true;
}
RegionSelect.prototype.onSelect = function(evt){
var _self = this;
if(_self.isSelect){
if(_self.selectDiv.style.display == "none") _self.selectDiv.style.display = "";
var posX = posXY(evt).x;
var poxY = posXY(evt).y;
_self.selectDiv.style.left = Math.min(posX, this.startX);
_self.selectDiv.style.top = Math.min(poxY, this.startY);
_self.selectDiv.style.width = Math.abs(posX - this.startX);
_self.selectDiv.style.height = Math.abs(poxY - this.startY);
var regionList = _self.regions;
for(var i=0; i< regionList.length; i++){
var r = regionList[i], sr = _self.innerRegion(_self.selectDiv,r);
if(sr && r.className.indexOf(_self.selectedClass) == -1){
r.className = r.className + " "+_self.selectedClass;
_selectedRegions.push(r);
}else if(!sr && r.className.indexOf(_self.selectedClass) != -1){
r.className = r.className.replaceAll(_self.selectedClass,"");
_selectedRegions.remove(r);
}
}
}
}
RegionSelect.prototype.onEnd = function(){
if(this.selectDiv){
this.selectDiv.style.display = "none";
}
this.isSelect = false;
//_selectedRegions = this.selectedRegion;
}
// 判断一个区域是否在选择区内
RegionSelect.prototype.innerRegion = function(selDiv, region){
var s_top = parseInt(selDiv.style.top);
var s_left = parseInt(selDiv.style.left);
var s_right = s_left + parseInt(selDiv.offsetWidth);
var s_bottom = s_top + parseInt(selDiv.offsetHeight);
var r_top = parseInt(region.offsetTop);
var r_left = parseInt(region.offsetLeft);
var r_right = r_left + parseInt(region.offsetWidth);
var r_bottom = r_top + parseInt(region.offsetHeight);
var t = Math.max(s_top, r_top);
var r = Math.min(s_right, r_right);
var b = Math.min(s_bottom, r_bottom);
var l = Math.max(s_left, r_left);
if (b > t+5 && r > l+5) {
return region;
} else {
return null;
}
}
RegionSelect.prototype.clearSelections = function(regions){
for(var i=0; i<regions.length;i++){
regions[i].className = regions[i].className.replaceAll(this.selectedClass,"");
}
}
function getSelectedRegions(){
return _selectedRegions;
}
JavaScript简单实现鼠标拖动选择功能(2)
内容版权声明:除非注明,否则皆为本站原创文章。