OpenLayers学习7-工具类Util_4 Posted on 2015-02-02 | In OpenLayers学习 | 一:尺寸计算123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157//计算出popup的尺寸, left值会被写成-9999px, 防止滚动条闪烁//size包含w 和 h参数, //option: displayClass是css类名, containerElement是父对象OpenLayers.Util.getRenderedDimensions = function(contentHTML, size, options) { var w, h; //临时div对象的容器 var container = document.createElement("div"); container.style.visibility = "hidden"; //popup容器优先是options.containerElement, 然后是body var containerElement = (options && options.containerElement) ? options.containerElement : document.body; // IE7不能从父对象继承position:aboslute var parentHasPositionAbsolute = false; var superContainer = null; var parent = containerElement; while (parent && parent.tagName.toLowerCase()!="body") { //获取父对象的position值 var parentPosition = OpenLayers.Element.getStyle(parent, "position"); if(parentPosition == "absolute") { parentHasPositionAbsolute = true;//父对象是absolute标记 break; } else if (parentPosition && parentPosition != "static") { break; } parent = parent.parentNode; } if(parentHasPositionAbsolute && (containerElement.clientHeight === 0 || containerElement.clientWidth === 0) ){ //不能继承aboslute就手工在外边套一层 superContainer = document.createElement("div"); superContainer.style.visibility = "hidden"; superContainer.style.position = "absolute"; superContainer.style.overflow = "visible"; superContainer.style.width = document.body.clientWidth + "px"; superContainer.style.height = document.body.clientHeight + "px"; superContainer.appendChild(container); } container.style.position = "absolute"; //写入指定的w和h if (size) { if (size.w) { w = size.w; container.style.width = w + "px"; } else if (size.h) { h = size.h; container.style.height = h + "px"; } } //写入指定css类名 if (options && options.displayClass) { container.className = options.displayClass; } //内容div var content = document.createElement("div"); content.innerHTML = contentHTML; //计算尺寸时需要overflow: visible content.style.overflow = "visible"; if (content.childNodes) { for (var i=0, l=content.childNodes.length; i<l; i++) { if (!content.childNodes[i].style) continue; content.childNodes[i].style.overflow = "visible";//子元素overflow都改成visible } } //把内容div写入临时容器 container.appendChild(content); //把临时容器写入页面 if (superContainer) { containerElement.appendChild(superContainer); } else { containerElement.appendChild(container); } //没指定w的话 if (!w) { w = parseInt(content.scrollWidth); //宽就是scrollWidth container.style.width = w + "px"; } //记录下来h if (!h) { h = parseInt(content.scrollHeight); } //遍历删除子元素 container.removeChild(content); if (superContainer) { superContainer.removeChild(container); containerElement.removeChild(superContainer); } else { containerElement.removeChild(container); } return new OpenLayers.Size(w, h);//返回w 和 h}; //获得滚动条宽度OpenLayers.Util.getScrollbarWidth = function() { //默认宽度 初始化时没有, 在这个函数末尾追加 var scrollbarWidth = OpenLayers.Util._scrollbarWidth; if (scrollbarWidth == null) { //初始化计算 var scr = null; var inn = null; var wNoScroll = 0; var wScroll = 0; //外层div, 在视野外计算 scr = document.createElement('div'); scr.style.position = 'absolute'; scr.style.top = '-1000px'; scr.style.left = '-1000px'; scr.style.width = '100px';//比较小 scr.style.height = '50px'; //没有滚动条 scr.style.overflow = 'hidden'; //内层, 写死大小 inn = document.createElement('div'); inn.style.width = '100%'; inn.style.height = '200px';//比较大 //外层套内层 scr.appendChild(inn); //body套外层 document.body.appendChild(scr); //原始内层的实际宽度offsetWidth(包含滚动条宽度) wNoScroll = inn.offsetWidth; //外层显示滚动条 scr.style.overflow = 'scroll'; //有滚动条以后的实际宽度offsetWidth wScroll = inn.offsetWidth; //计算完了, 删掉div document.body.removeChild(document.body.lastChild); //记录结果 OpenLayers.Util._scrollbarWidth = (wNoScroll - wScroll);//内层没滚动条的宽度 - 有滚动条的宽度 scrollbarWidth = OpenLayers.Util._scrollbarWidth; } return scrollbarWidth;};