`
robinqu
  • 浏览: 88674 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Javascript CSS编程 (一)元素定位、透明、内联样式

阅读更多
Querying Element Position and Size
查询元素坐标和大小

引用
the offsetLeft and offsetTop properties of an element return the X and Y coordinates of the element.

the offsetWidth and offsetHeight properties return the width and height. These properties are read-only and return pixel values as numbers (not as CSS strings with "px" units appended).

They mirror the CSS left, top, width, and height attributes

offesetLeft and offsetTop specify the X and the Y coordinates of an element relative to offsetParent element.

For positioned elements, the offsetParent is typically the <body> tag or the <html> tag (which has an offsetParent of null) or a positioned ancestor of the positioned element. For nonpositioned elements, different browsers handle the offsetParent differently.

In general, therefore, the portable way to determine the position of an element is to loop through the offsetParent references, accumulating offsets.


对于已定为的元素,offsetLeft\offsetTop返回元素的X、Y坐标。

offsetWidth、offsetHeight返回宽和高。返回数值只有数值而没有单位。

这些属性分别对性CSS的left、top、width、height属性。

offsetTop、offsetLeft的是相对于offsetParent的。
对于已经定位过的元素,offsetParent通常是body标签或html标签(通常这两个标签的offsetParent值是null);对于没有定位过的,offsetParent就是一个已经定位过的祖先。

对于未定位
// Get the X coordinate of the element e.
function getX(e) {
    var x = 0;                // Start with 0
    while(e) {                // Start at element e
        x += e.offsetLeft;    // Add in the offset
        e = e.offsetParent;   // And move up to the offsetParent
    }
    return x;                 // Return the total offsetLeft
}


下面这个方法就考虑了滚动条的存在,这个是比较健壮的版本:
function getY(element) {
    var y = 0;
    for(var e = element; e; e = e.offsetParent) // Iterate the offsetParents
        y += e.offsetTop;                       // Add up offsetTop values

    // Now loop up through the ancestors of the element, looking for
    // any that have scrollTop set. Subtract these scrolling values from
    // the total offset. However, we must be sure to stop the loop before
    // we reach document.body, or we'll take document scrolling into account
    // and end up converting our offset to window coordinates.
    for(e = element.parentNode; e && e != document.body; e = e.parentNode)
        if (e.scrollTop) y -= e.scrollTop;  // subtract scrollbar values

    // This is the Y coordinate with document-internal scrolling accounted for.
    return y;
}


Translucency
透明

每个浏览器实现透明度方法不一样,所以CSS会很麻烦:
opacity: .75;               /* standard CSS3 style for transparency */
-moz-opacity: .75;          /* transparency for older Mozillas */
filter: alpha(opacity=75);  /* transparency for IE; note no decimal point */


Scripting Inline Styles
内联样式编程

引用
It is important to understand that the CSS2Properties object you obtain with the style property of an element specifies only the inline styles of the element. You cannot use the properties of the CSS2Properties object to obtain information about the stylesheet styles that apply to the element. By setting properties on this object, you are defining inline styles that effectively override stylesheet styles.


你通过元素的stlye属性得到的CSS2Properties对象只包含内联样式,你无法通过这个属性访问到整个样式表。但是通过设定这个属性,你是可以强制更改这个元素的属性的,因为内联样式的优先级是最高的。

在编写内联样式的时候,要注意驼背命名法,以及一些特殊的CSS名称的转换,比如:
float - > cssFloat
分享到:
评论

相关推荐

    JavaScript权威指南(第6版)中文版pdf+源代码

     16.3 脚本化内联样式427  16.4 查询计算出的样式431  16.5 脚本化CSS类433  16.6 脚本化样式表436  第17章 事件处理440  17.1 事件类型442  17.2 注册事件处理程序451  17.3 事件处理程序的调用454  17.4 ...

    JavaScript权威指南(第6版)(附源码)

    16.3 脚本化内联样式 16.4 查询计算出的样式 16.5 脚本化CSS类 16.6 脚本化样式表 第17章 事件处理 17.1 事件类型 17.2 注册事件处理程序 17.3 事件处理程序的调用 17.4 文档加载事件 17.5 鼠标事件 17.6 鼠标滚轮...

    JavaScript权威指南(第6版)

    16.3 脚本化内联样式 16.4 查询计算出的样式 16.5 脚本化CSS类 16.6 脚本化样式表 第17章 事件处理 17.1 事件类型 17.2 注册事件处理程序 17.3 事件处理程序的调用 17.4 文档加载事件 17.5 鼠标事件 17.6 鼠标滚轮...

    JavaScript权威指南(第6版)中文文字版

    16.3 脚本化内联样式 427 16.4 查询计算出的样式 431 16.5 脚本化css类 433 16.6 脚本化样式表 436 第17章 事件处理 440 17.1 事件类型 442 17.2 注册事件处理程序 451 17.3 事件处理程序的调用 454 17.4 文档加载...

    JavaScript 权威指南(第四版).pdf

     16.3 脚本化内联样式427  16.4 查询计算出的样式431  16.5 脚本化CSS类433  16.6 脚本化样式表436  第17章 事件处理440  17.1 事件类型442  17.2 注册事件处理程序451  17.3 事件处理程序的调用454  17.4 ...

    JavaScript权威指南(第6版)(中文版)

    16.3 脚本化内联样式 16.4 查询计算出的样式 16.5 脚本化CSS类 16.6 脚本化样式表 第17章 事件处理 17.1 事件类型 17.2 注册事件处理程序 17.3 事件处理程序的调用 17.4 文档加载事件 17.5 鼠标事件 17.6 鼠标滚轮...

    JavaScript详解(第2版)

     14.5.2 内联样式和〈style〉属性   14.6 链接的样式表   14.6.1 〈link〉标签   14.6.2 使用@import导入   14.7 创建样式类   14.7.1 用类设定简单表格样式   14.7.2 使用特定类选择器   ...

    JavaScript权威指南(第6版)

    16.3 脚本化内联样式 427 16.4 查询计算出的样式 431 16.5 脚本化css类 433 16.6 脚本化样式表 436 第17章 事件处理 440 17.1 事件类型 442 17.2 注册事件处理程序 451 17.3 事件处理程序的调用 454 17.4 文档加载...

    JavaScript权威指南(第6版) 中文版

    16.3 脚本化内联样式 427 16.4 查询计算出的样式 431 16.5 脚本化css类 433 16.6 脚本化样式表 436 第17章 事件处理 440 17.1 事件类型 442 17.2 注册事件处理程序 451 17.3 事件处理程序的调用 454 17.4 文档加载...

    JavaScript权威指南(第六版) 清晰-完整

    16.3 脚本化内联样式 16.4 查询计算出的样式 16.5 脚本化CSS类 16.6 脚本化样式表 第17章 事件处理 17.1 事件类型 17.2 注册事件处理程序 17.3 事件处理程序的调用 17.4 文档加载事件 17.5 鼠标事件 17.6 鼠标滚轮...

    精通JS脚本之ExtJS框架.part2.rar

    本书共分17章,分别介绍了JavaScript的对象编程、JavaScript浏览器对象模型和事件机制、ExtJS的核心类库和组件、ExtJS的事件处理方式、设计进度条、设计工具栏和菜单栏、设计面板、设计表格、设计表单、设计数据表、...

    精通JS脚本之ExtJS框架.part1.rar

    本书共分17章,分别介绍了JavaScript的对象编程、JavaScript浏览器对象模型和事件机制、ExtJS的核心类库和组件、ExtJS的事件处理方式、设计进度条、设计工具栏和菜单栏、设计面板、设计表格、设计表单、设计数据表、...

    ASP.NET4高级程序设计第4版 带目录PDF 分卷压缩包 part1

    手动优化了PDF的书签,书签可折叠,书签链接以目录方式保存,多达1000多页,每页都做了书签定位,手都累酸啦。 ============================== 因权限只能到60MB,分卷压缩了,共3个压缩包,需下载完3个一起解压, ...

Global site tag (gtag.js) - Google Analytics