最新日志

getBoundingClientRect() 来获取页面元素的位置

http://www.cnblogs.com/qieqing/archive/2008/10/06/1304399.html
document.documentElement.getBoundingClientRect

下面这是MSDN的解释:

Syntax
oRect = object.getBoundingClientRect()
Return Value
Returns a TextRectangle object. Each rectangle has four integer properties (top, left, right, and bottom) that represent a coordinate of the rectangle, in pixels.
Remarks
This method retrieves an object that exposes the left, top, right, and bottom coordinates of the union of rectangles relative to the client's upper-left corner. In Microsoft Internet Explorer 5, the window's upper-left is at 2,2 (pixels) with respect to the true client.

还是实际解释下,该方法获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置。也不好理解,下面用图说明下。

该方法已经不再是IE Only了,FF3.0+和Opera9.5+已经支持了该方法,可以说在获得页面元素位置上效率能有很大的提高,在以前版本的Opera和Firefox中必须通过循环来获得元素在页面中的绝对位置。
按此在新窗口浏览图片
按此在新窗口浏览图片
下面的代码举了个简单的例子,可以滚动滚动条之后点红色区域看各个值的变化:


有了这个方法,获取页面元素的位置就简单多了

var X= this.getBoundingClientRect().left+document.documentElement.scrollLeft;

var Y=this.getBoundingClientRect().top+document.documentElement.scrollTop;

纯css效果

PS:只能看效果,本人觉得.应用到网站还是用JS比较好

缓冲效果

菜鸟版代码如下:
理解这段代码就基本上掌握了
copycode:
function f_s() {
var obj = document.getElementById("top");
obj.style.display = "block";
obj.style.height = "1px";

var sw = function () {
var s_width = parseInt(obj.style.height);
if (s_width < 350) {
obj.style.height = (s_width + Math.ceil((350 - s_width) / 15)) + "px";
}
else {
clearInterval(st);
}
}
var st = window.setInterval(sw, 1);
}



中级版本
http://bbs.blueidea.com/viewthread.php?tid=2843406&page=1#pid3929392
copycode:
/*
函数名称: Scroll
Scroll(obj, h, s)
参数说明:
obj,[object] id值或对象. 必需
h,[height] 展开后的高度. 可选(默认为200px)
s,[speed] 展开速度,值越小展开速度越慢. 可选(默认为1.2){建议取值为1.1到2.0之间[例如:1.17]}.
函数返回值:
true 展开(对象的高度等于展开后的高度)
false 关闭(对象的高度等于原始高度)
*/
function Scroll(obj, h, s){
if(obj == undefined){return false;}
var h = h || 200;
var s = s || 1.2;
var obj = typeof(obj)=="string"?document.getElementById(obj):obj;
var status = obj.getAttribute("status")==null;
var oh = parseInt(obj.offsetHeight);
obj.style.height = oh;
obj.style.display = "block";
obj.style.overflow = "hidden";
if(obj.getAttribute("oldHeight") == null){
obj.setAttribute("oldHeight", oh);
}else{
var oldH = Math.ceil(obj.getAttribute("oldHeight"));
}
var reSet = function(){
if(status){
if(oh < h){
oh = Math.ceil(h-(h-oh)/s);
obj.style.height = oh+"px";
}else{
obj.setAttribute("status",false);
window.clearInterval(IntervalId);
}
}else{
obj.style.height = oldH+"px";
obj.removeAttribute("status");
window.clearInterval(IntervalId);
}
}
var IntervalId = window.setInterval(reSet,10);
return status;
}



高级版本
这个很全,不过,我是没有看懂的.- -!!
http://www.cnblogs.com/cloudgamer/



附高级版本:下载

页面的各种尺寸

copycode:
scrollHeight: 获取对象的滚动高度。
scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离
scrollWidth:获取对象的滚动宽度
offsetHeight:获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的高度
offsetLeft:获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算左侧位置
offsetTop:获取对象相对于版面或由 offsetTop 属性指定的父坐标的计算顶端位置
event.clientX 相对文档的水平座标
event.clientY 相对文档的垂直座标
event.offsetX 相对容器的水平坐标
event.offsetY 相对容器的垂直坐标
document.documentElement.scrollTop 垂直方向滚动的值
event.clientX+document.documentElement.scrollTop 相对文档的水平座标+垂直方向滚动的量


例子详解:



xhtml不支持document.body.scrollTop
当为html文档加上如下头以支持xhtml过渡标准时候,使用document.body.scrollTop值始终为0.这是document.body.scrollTop始终为0,这时需要用document.documentElement.scrollTop才能获得正确的值,而如果不加xhtml的申明,document.documentElement.scrollTop将为0

scrollTop的一些事
用它可以来做一些网页的滚动效果.经常见到的是图片游览.
比如,简单的这个:


看这段代码:
copycode:
function up(){
var a=$("img_scroll_box").scrollTop;
var b=$("img_scroll_box").scrollTop;
while(a>b-58){
a=a-6;
$("img_scroll_box").scrollTop=a;}}

关于scrollTop在前面也已有详解.主要是根据当前的scrollTop的大小来确定下一次scrollTop的大小.根据这一点.可以简单的做一个滚动效果的.如下:


分享一些高手们的技巧
scroll滚动的封装
(1)http://www.css88.com


(2)这个版本是朋友给,没有地址,所以保留作者.
这里有2个封装.很强大.
copycode:
//<!-作者:风飘无影 -->
//参数说明: 要滚动的容器ID,方向r,l,t,b(t,b要注意内容必须是多行),延时速度,宽度,高度
function $(id){return document.getElementById(id);}
function myMarquee(_id,_mode,_speed,_w,_h)
{
this.ID=_id;
this.mode=_mode;
this.speed=_speed;
this.txt=$(_id).innerHTML;
this.width=_w;
this.height=_h;
var s='<div id="'+_id+'content" style="overflow: hidden;height:'+_h+'px;width:'+_w+'px;white-space: nowrap;"><div id="'+_id+'_show" style="display: inline">'+this.txt+'</div><div id="'+_id+'_hide" style="display: inline"></div></div>'
$(_id).innerHTML=s;
}

myMarquee.prototype.run=function()
{

$(this.ID+'_hide').innerHTML=$(this.ID+'_show').innerHTML;
var temp='mymarquee'+this.ID+'=setInterval(\'doing("'+this.mode+'","'+this.ID+'")\','+this.speed+')'
eval(temp)

var _id=this.ID;
var _mode=this.mode;
var _speed=this.speed;
$(this.ID+'content').onmouseover=function()
{
eval('clearInterval(mymarquee'+_id+')')
}
$(this.ID+'content').onmouseout=function()
{
eval('mymarquee'+_id+'=setInterval(\'doing("'+_mode+'","'+_id+'")\',_speed)');
}
}

function doing(mode,ID)
{
// alert(ID)
switch(mode) {
case 'l':
if($(ID+'_show').offsetWidth-$(ID+'content').scrollLeft<=0)
$(ID+'content').scrollLeft-=$(ID+'_show').offsetWidth
else{
$(ID+'content').scrollLeft++
}
break;
case 'r':
if($(ID+'content').scrollLeft<=0)
$(ID+'content').scrollLeft+=$(ID+'_show').offsetWidth
else{
$(ID+'content').scrollLeft--
}
break;
case 't':
if($(ID+'_hide').offsetTop-$(ID+'content').scrollTop<=0)
$(ID+'content').scrollTop-=$(ID+'_show').offsetHeight
else{
$(ID+'content').scrollTop++
}
break;
case 'b':
if($(ID+'_show').offsetTop-$(ID+'content').scrollTop>=0)
$(ID+'content').scrollTop+=$(ID+'_hide').offsetHeight
else{
$(ID+'content').scrollTop--
}
break;
}
}

实例2应用下载:下载

事件event的一些事儿

之前在在试做一个小模块,刚开始做的还挺顺利,到后来开始出现了一些小问题,是关于
Focus和blur的焦点事件.不过写篇文章不是说这个处理结果.因为很多东西都是要必须经常看到.像这些:
先看JS手册里的描述(以下信息来自引用);

event 对象:
代表事件状态,如事件发生的元素,键盘状态,鼠标位置和鼠标按钮状态。代表事件状态,如事件发生的元素,键盘状态,鼠标位置和鼠标按钮状态。
比较常见的属性有:
altKey: 设置或获取 Alt 键的状态
altLeft 设置或获取左 Alt 键的状态。
button 设置或获取用户所按的鼠标按钮。
clientX 设置或获取鼠标指针位置相对于窗口客户区域的 x 坐标,其中客户区域不包括窗口自身的控件和滚动条。
clientY 设置或获取鼠标指针位置相对于窗口客户区域的 y 坐标,其中客户区域不包括窗口自身的控件和滚动条.
ctrlKey 设置或获取 Ctrl 键的状态。
ctrlLeft 设置或获取左 Ctrl 键的状态
keyCode 设置或获取与导致事件的按键关联的 Unicode 按键代码。
offsetX 设置或获取鼠标指针位置相对于触发事件的对象的 x 坐标。
offsetY 设置或获取鼠标指针位置相对于触发事件的对象的 y 坐标。
screenX 设置或获取获取鼠标指针位置相对于用户屏幕的 x 坐标。
screenY 设置或获取鼠标指针位置相对于用户屏幕的 y 坐标。
shiftKey 设置或获取 Shift 键的状态。
shiftLeft 设置或获取左 Shift 键的状态。
srcElement 设置或获取触发事件的对象。

事件响应
在事件参数问题上,IE和DOM leve-2标准不同,在后者的标准中,event对象是被作为事件接收函数(闭包)的参数传递的,而在IE中则是作为window对象(全局)的属性.所在首先处理兼容;



copycode:
function Fe(event1){
event1=(event1) || window.event;//兼容两种游览器的event参数模式
var target=event1.srcElement || event1.target;//兼容两种游览器的event参数属性
alert(target.type);

}

常见表单中的Enter+ctrl提交
copycode:
<script type="text/javascript">
window.onload=document.getElementById("bb1").onkeydown=function bb(evt){
evt=evt || window.event;
if(evt.keyCode==13 && evt.ctrlKey)//这里你可用shiftKey,altKey来进行一些其它事件模式
{alert("okkk");}}
</script>

禁右键
copycode:
<script>
document.oncontextmenu=function(e){
if(document.all){ return false; }else{ e.preventDefault();}
}
</script>



上面引用event其它属性:


其它应用