控制iframe的滚动 和 iframe的透明背景研究比较
控制iframe的滚动
如何控制框架页的滚动
解决思路:
利用框架文档中window对象的scrollBy方法来滚动。
具体步骤:
1. 包含框架页的代码。
View Code
1 <script>
2 var itv,stepX,stepY,obj
3 function scrollStart(x,y){
4 stepX=x //X轴方向上的偏移量
5 stepY=y //Y轴方向上的偏移量
6 obj=document.frames.demo //捕获框架对象
7 //设置间隔事件,每10毫秒以stepX和stepY为偏移量滚动一次
8 itv=setInterval("obj.scrollBy(stepX,stepY)",10)
9 }
10 function scrollStop(){
11 clearInterval(itv) //取消间隔事件,达到停止滚动的效果
12 }
13 </script>
14 <iframe frameborder="0" scrolling="no" name="demo"
15 src="demo.htm"></iframe>
16 <button onmouseover="scrollStart(0,-1)"
17 onmouseout="scrollStop()" onmousedown="stepY=-5"
18 onmouseup="stepY=-1">上</button>
19 <button onmouseover="scrollStart(0,1)"
20 onmouseout="scrollStop()" onmousedown="stepY=5"
21 onmouseup="stepY=1">下</button>
22 <button onmouseover="scrollStart(-1,0)"
23 onmouseout="scrollStop()" onmousedown="stepX=-5"
24 onmouseup="stepX=-1">左</button>
25 <button onmouseover="scrollStart(1,0)"
26 onmouseout="scrollStop()" onmousedown="stepX=5"
27 onmouseup="stepX=1">右</button>
2.demo.htm页代码。这里仅仅是为了测试效果,可以替换为你自己的页面。
View Code
1 <script>
2 //为了产生横向滚动条
3 document.write(new Array(100).join("1 "))
4 //为了产生纵向滚动条
5 document.write(new Array(100).join("1<br>"))
6 </script>
注意:如果iframe所加载的页为站外URL,将导致跨域问题,拒绝访问。凡是涉及到对框架页的访问及控制,都会有跨域问题。
特别提示
代码运行后的效果如图1.6.8所示。鼠标移上四上按钮上后,iframe内所加载的页面将分别向上、下、左和右四个方面滚动,在按下鼠标时滚动速度加快,松开鼠标(仍然在按钮上)时恢复速度,鼠标移开后滚动停止。
特别说明
本例主要是window对象的scrollBy方法的应用。通过设置横向滚动速度stepX和纵向滚动速度stepY为全局变量,在鼠标移上时在函数中用setInterval不断调用scrollBy方法滚动页面,通过参数控制滚动方向,在鼠标按下时放大全局变量stepX或stepY的值,人而达到加快滚动速度的效果,而鼠标移开后再用clearInterval方法清除之前的setInterval事件以停止滚动。
scrollBy 将窗口滚动 x 和 y 偏移量。
setInterval 每经过指定毫秒值后计算一个表达式。
clearInterval 使用 setInterval 方法取消先前开始的间隔事件。
iframe的透明背景研究比较
在transparentBody.htm文件的<body>标签中,我已经加入了style="background-color=transparent" 通过以下四种IFRAME的写法我想大概你对iframe背景透明效果的实现方法应该会有个清晰的了解:
1 <IFRAME ID="Frame1" SRC="transparentBody.htm" allowTransparency="true"></IFRAME>
2
3
4 <IFRAME ID="Frame2" SRC="transparentBody.htm" allowTransparency="true" STYLE="background-color: green"> </IFRAME>
5
6
7 <IFRAME ID="Frame3" SRC="transparentBody.htm"></IFRAME>
8
9
10 <IFRAME ID="Frame4" SRC="transparentBody.htm" STYLE="background-color: green"> </IFRAME>