一个刚完成的layout(拖动流畅,不受iframe影响)_javascript技巧
写一个layout本来是一个很简单的事情,可这次的一个layout问题确让我为难了许久才做出来,下面来大概讲解一下问题的出现与解决过程。
注:本文代码皆基于jquery实现。
按照普通的方法写一个layout,一般是用一个table来实现,用中间的td拖动来控制左右两个td的大小,这个问题简单,很快就搞定。代码如下:
QUOTE:
nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script></script>
<script> <BR> $(document).ready(function(){ <BR> //及时调整页面内容的高度 <BR> setInterval(function(){ <BR> var winH=(document.documentElement||document.body).clientHeight; <BR> $("#tbl,#sideBar,#toggleBar,#main").css("height",winH); <BR> $("td").each(function(){$(this).html()||$(this).html(" ")}); <BR> },100) <BR> } <BR> ); <br><br> var begin_x; <BR> var drag_flag = false; <BR> document.onmousemove = mouseDrag <BR> document.onmouseup = mouseDragEnd <BR> //半透明拖动条 <BR> var alphaDiv="<div class='div' id='alphaDiv' style='position:absolute;height:2000px;top:0;z-index:10001;filter:alpha(opacity=50);opacity:0.5;left:200px'> "; <BR> function setDrag(){ <BR> drag_flag=true; <BR> begin_x=event.x; <BR> //添加半透明拖动条 <BR> $(alphaDiv).css("left",$("#toggleBar")[0].offsetLeft).appendTo("body"); <BR> } <br><br> //拖动时执行的函数 <BR> function mouseDrag(){ <BR> if(drag_flag==true){ <BR> if (window.event.button==1){ <BR> var now_x=event.x; <BR> var value=parseInt($("#alphaDiv")[0].style.left)+now_x-begin_x; <BR> $("#alphaDiv")[0].style.left=value+"px"; <BR> begin_x=now_x; <BR> } <BR> $("body").css("cursor","e-resize"); //设定光标类型 <BR> }else{ <BR> try{ <BR> $("#sideBar")[0].style.pixelWidth=$("#alphaDiv")[0].style.left; <BR> $("#alphaDiv").remove(); <BR> }catch(e){} <BR> } <BR> } <br><br> function mouseDragEnd(){ <BR> //设置拖动条的位置 <BR> if(drag_flag==true){ <BR> //设定拖动条的位置(设定左侧的宽度) <BR> $("#sideBar")[0].style.pixelWidth=parseInt($("#alphaDiv")[0].style.left); <BR> $("#alphaDiv").remove(); //删除半透明拖动条 <BR> $("body").css("cursor","normal"); //恢复光标类型 <BR> } <BR> drag_flag=false; <BR> } <BR> </script>
asdfasdf | right Panel |
演示地址:http://www.ajaxbbs.net/test/layout/JqSplit/noiframe.htm上面的这种写法也是大多数layout的写法,著名框架dojo好像也是这么实现的,其他的没试。
但现在的情况仍然不能满足我们的需求,我们需要左侧或右侧是ifame,通过iframe调用相关的页面,在前面的代码中将右侧改为iframe。
演示地址:http://www.ajaxbbs.net/test/layout/JqSplit/iframeRight.htm
这时我们就发现问题了,只能向左边拖动,但不能像右边拖动,这是为什们呢?
经过检查,发现原来当鼠标移动到iframe上就无法捕获鼠标的位置了,event对象也不存在。得不到鼠标的位置我们的拖动当然会出现问题了。
这个问题着实让我郁闷了许久,然后测试其他的一些layout(对iframe进行了处理)发现凡是使用iframe的都有一个缺陷,当鼠标拖动速度很快的时候,拉动条速度跟不上(当然这些并没有那个模拟的半透明的拖动条,直接拖动真实的拖动条的),感觉就是很不流畅很不同步。
我们看一下直接拖动真是滚动条的情况
演示地址:http://www.ajaxbbs.net/test/layout/JqSplit/iframeRightNoAlpha.htm我们慢速度拖动还是可以向右移动的,但一但速度稍快便不能拖动了。
对于这个问题始终没有想到好的解决办法,就在我悲伤的即将放弃时,看到前几天写的一个模拟弹出框,因为当时测试弹出框应该要遮住包括iframe在内的select。所以页面中使用了ifame。突然发现一个索引很高的层能够遮住iframe,突然间就有了灵感,马上实验。
思路如下:拖动拉条时在页面添加一个索引很大的层(如10000),将其透明度设为0(完全透明),这样鼠标就不会移动到iframe中,但iframe仍然存在可以看到。当拖动结束(onmouseup)时去掉这个层即可,这样就实现了比较完美的拖动。
演示地址:http://www.ajaxbbs.net/test/layout/JqSplit/demo.htm我们看一下完整的代码:
QUOTE:
nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script></script>
<script> <BR> $(document).ready(function(){ <BR> //及时调整页面内容的高度 <BR> setInterval(function(){ <BR> var winH=(document.documentElement||document.body).clientHeight; <BR> $("#tbl,#sideBar,#toggleBar,#main").css("height",winH); <BR> $("td").each(function(){$(this).html()||$(this).html(" ")}); <BR> },100) <BR> } <BR> ); <br><br> var begin_x; <BR> var drag_flag = false; <BR> document.onmousemove = mouseDrag <BR> document.onmouseup = mouseDragEnd <BR> //半透明的拖动条(模拟) <BR> var alphaDiv="<div class='div' id='alphaDiv' style='position:absolute;height:2000px;top:0;z-index:10001;filter:alpha(opacity=50);opacity:0.5;left:200px'> "; <BR> function setDrag(){ <BR> drag_flag=true; <BR> begin_x=event.x; <BR> //添加蒙板 <BR> createMask(); <BR> //添加半透明拖动条 <BR> $(alphaDiv).css("left",$("#toggleBar")[0].offsetLeft).appendTo("body"); <BR> } <br><br> //关键部分 <BR> function createMask(){ <BR> //创建背景 <BR> var rootEl=document.documentElement||document.body; <BR> var docHeight=((rootEl.clientHeight>rootEl.scrollHeight)?rootEl.clientHeight:rootEl.scrollHeight)+"px"; <BR> var docWidth=((rootEl.clientWidth>rootEl.scrollWidth)?rootEl.clientWidth:rootEl.scrollWidth)+"px"; <BR> var shieldStyle="position:absolute;top:0px;left:0px;width:"+docWidth+";height:"+docHeight+";background:#000;z-index:10000;filter:alpha(opacity=0);opacity:0"; <BR> $("<div id='shield' style=\""+shieldStyle+"\">").appendTo("body"); <BR> } <BR> //拖动时执行的函数 <BR> function mouseDrag(){ <BR> if(drag_flag==true){ <BR> if (window.event.button==1){ <BR> var now_x=event.x; <BR> var value=parseInt($("#alphaDiv")[0].style.left)+now_x-begin_x; <BR> $("#alphaDiv")[0].style.left=value+"px"; <BR> begin_x=now_x; <BR> } <BR> $("body").css("cursor","e-resize"); //设定光标类型 <BR> }else{ <BR> try{ <BR> $("#shield").remove(); <BR> $("#sideBar")[0].style.pixelWidth=$("#alphaDiv")[0].style.left; <BR> $("#alphaDiv").remove(); <BR> }catch(e){} <BR> } <BR> } <br><br> function mouseDragEnd(){ <BR> //设置拖动条的位置 <BR> if(drag_flag==true){ <BR> //设定拖动条的位置(设定左侧的宽度) <BR> $("#sideBar")[0].style.pixelWidth=parseInt($("#alphaDiv")[0].style.left); <BR> $("#shield").remove(); //删除蒙板 <BR> $("#alphaDiv").remove(); //删除半透明拖动条 <BR> $("body").css("cursor","normal"); //恢复光标类型 <BR> } <BR> drag_flag=false; <BR> } <BR> </script>
asdfasdf | |
自己的一点发现,一点心得,不知对大家有没有用处,只管拿出来献丑了!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
