`
hrsvici412
  • 浏览: 75048 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

精通JavaScript源码2

阅读更多
// Find the left position of an element
function posX(elem) {
    // Get the computed style and get the number out of the value
    return parseInt( getStyle( elem, “left” ) );
}
// Find the top position of an element
function posY(elem) {
    // Get the computed style and get the number out of the value
    return parseInt( getStyle( elem, “top” ) );
}

 

获取style指定elem的值:

function getStyle( elem, name ) {
    if (elem.style[name])
        return elem.style[name];
     //IE中的方法
    else if (elem.currentStyle)
        return elem.currentStyle[name];
    // W3C的方法
    else if (document.defaultView && document.defaultView.getComputedStyle) {
        // It uses the traditional ‘text-align’ style of rule writing, instead of textAlign
        name = name.replace(/([A-Z])/g,"-$1");
        name = name.toLowerCase();
        // Get the style object and get the value of the property (if it exists)
        var s = document.defaultView.getComputedStyle(elem,"");
        return s && s.getPropertyValue(name);
    } else
        return null;
}

找到elem在页面中水平X(left),垂直Y的值(top),offsetLeft方法在不同的浏览器有所不同。

// Find the X (Horizontal, Left) position of an element
function pageX(elem) {
    var p = 0;
    // We need to add up all of the offsets for every parent
    while ( elem.offsetParent ) {
        // Add the offset to the current count
        p += elem.offsetLeft;
        // and continue on to the next parent
        elem = elem.offsetParent;
    }
    return p;
}
// Find the Y (Vertical, Top) position of an element
function pageY(elem) {
    var p = 0;
    // We need to add up all the offsets for every parent
    while ( elem.offsetParent ) {
        // Add the offset to the current count
        p += elem.offsetTop;
        // and continue on to the next parent
        elem = elem.offsetParent;
    }
    return p;
}

  找到elem距离去父元素的位置:

// Find the horizontal positioing of an element within its parent
function parentX(elem) {
    // If the offsetParent is the element’s parent, break early
    return elem.parentNode == elem.offsetParent ?
        elem.offsetLeft :
        // Otherwise, we need to find the position relative to the entire
        // page for both elements, and find the difference
        pageX( elem ) - pageX( elem.parentNode );
}
// Find the vertical positioing of an element within its parent
function parentY(elem) {
    // If the offsetParent is the element’s parent, break early
    return elem.parentNode == elem.offsetParent ?
        elem.offsetTop :
        // Otherwise, we need to find the position relative to the entire
        // page for both elements, and find the difference
        pageY( elem ) - pageY( elem.parentNode );
}

 找到elem中style中left,top的值:

// Find the left position of an element
function posX(elem) {
    // Get the computed style and get the number out of the value
    return parseInt( getStyle( elem, “left” ) );
}
// Find the top position of an element
function posY(elem) {
    // Get the computed style and get the number out of the value
    return parseInt( getStyle( elem, “top” ) );
}

 设置elem left,top的值:

// A function for setting the horizontal position of an element
function setX(elem, pos) {
    // Set the ‘left’ CSS property, using pixel units
    elem.style.left = pos + “px”;
}
// A function for setting the vertical position of an element
function setY(elem, pos) {
    // Set the ‘left’ CSS property, using pixel units
    elem.style.top = pos + “px”;
}
// A function for adding a number of pixels to the horizontal
// position of an element 
function addX(elem,pos) {
    // Get the current horz. position and add the offset to it.
    setX( posX(elem) + pos );
}
// A function that can be used to add a number of pixels to the
// vertical position of an element
function addY(elem,pos) {
    // Get the current vertical position and add the offset to it
    setY( posY(elem) + pos );
}

  获得元素的width,height,隐藏的元素是不可以获得width和height,可以用fullHeight,fullWidth实现:

// Get the actual height (using the computed CSS) of an element
function getHeight( elem ) {
    // Gets the computed CSS value and parses out a usable number
    return parseInt( getStyle( elem, ‘height’ ) );
}
// Get the actual width (using the computed CSS) of an element
function getWidth( elem ) {
    // Gets the computed CSS value and parses out a usable number
    return parseInt( getStyle( elem, ‘width’ ) );
}

// Find the full, possible, height of an element (not the actual,
// current, height)
function fullHeight( elem ) {
    // If the element is being displayed, then offsetHeight
    // should do the trick, barring that, getHeight() will work
    if ( getStyle( elem, ‘display’ ) != ‘none’ )
        return elem.offsetHeight || getHeight( elem );

    // Otherwise, we have to deal with an element with a display
    // of none, so we need to reset its CSS properties to get a more
    // accurate reading
    var old = resetCSS( elem, {
        display: ‘’,
        visibility: ‘hidden’,
        position: ‘absolute’
    });

    // Figure out what the full height of the element is, using clientHeight
    // and if that doesn’t work, use getHeight
    var h = elem.clientHeight || getHeight( elem );

    // Finally, restore the CSS properties back to what they were
    restoreCSS( elem, old );

    // and return the full height of the element
    return h;
}

// Find the full, possible, width of an element (not the actual,
// current, width)
function fullWidth( elem ) {
    // If the element is being displayed, then offsetWidth
    // should do the trick, barring that, getWidth() will work
    if ( getStyle( elem, ‘display’ ) != ‘none’ )
        return elem.offsetWidth || getWidth( elem );
    // Otherwise, we have to deal with an element with a display
    // of none, so we need to reset its CSS properties to get a more
    // accurate reading
    var old = resetCSS( elem, {
        display: ‘’,
        visibility: ‘hidden’,
        position: ‘absolute’
    });
    // Figure out what the full width of the element is, using clientWidth
    // and if that doesn’t work, use getWidth
    var w = elem.clientWidth || getWidth( elem );
    // Finally, restore the CSS properties back to what they were
    restoreCSS( elem, old );
    // and return the full width of the element
    return w;
}
// A function used for setting a set of CSS properties, which
// can then be restored back again later
function resetCSS( elem, prop ) {
    var old = {};
    // Go through each of the properties
    for ( var i in prop ) {
        // Remember the old property value
        old[ i ] = elem.style[ i ];
        // And set the new value
        elem.style[ i ] = prop[i];
    }
    // Retun the set of changed values, to be used by restoreCSS
    return old;
}
// A function for restoring the side effects of the resetCSS function
function restoreCSS( elem, prop ) {
    // Reset all the properties back to their original values
    for ( var i in prop )
        elem.style[ i ] = prop[ i ];


 

 

分享到:
评论

相关推荐

    精通javascript书及源码

    学习JavaScript源码,可以从中学习到如何利用这些技术实现高效的数据处理。 异步编程是JavaScript的另一个核心主题。事件循环、回调函数、Promise、async/await等技术用于解决非阻塞I/O问题。理解和运用这些机制,...

    精通JavaScript(源代码) jQuery之父John Resig所写

    总的来说,《精通JavaScript(源代码)》是一本深度和广度兼具的JavaScript进阶教程,它不仅涵盖了语言基础,更深入到jQuery的源码解析,让读者能够在理解JavaScript的基础上,更好地应用和扩展jQuery,从而提升Web...

    精通JavaScript源代码.rar

    《精通JavaScript》这本书深入浅出地介绍了JavaScript的各个方面,旨在帮助读者从新手进阶到高级开发者。其附带的源代码压缩包“精通JavaScript.rar”包含了381个示例,覆盖了书中讲解的各种技术点。 在JavaScript...

    精通JavaScript源码1

    "精通JavaScript源码1"这个主题,显然是要深入探讨JavaScript的内部机制和核心原理,帮助开发者从底层理解这门语言。 源码分析对于任何程序员来说都是至关重要的,它能帮助我们理解代码是如何运行的,从而提高我们...

    精通JavaScript+jQuery曾顺

    精通JavaScript+jQuery 曾顺 配套有书中的列子

    精通javascript的源代码

    本资源“精通javascript的源代码”旨在帮助学习者通过实例深入理解JavaScript,从而更好地掌握这门语言。 在JavaScript的学习过程中,源代码起着至关重要的作用。源代码是程序的基础,它展示了如何使用语言语法、...

    精通JavaScriptJquery源码

    精通 JavaScript Jquery 随书实例源码

    精通Javascript+jQuery 源码 免费

    在提供的压缩包中,"精通Javascript+jQuery" 可能包含了多个章节的源码示例,这些例子覆盖了从基础到进阶的各种主题。你可以逐个研究这些代码,理解每个功能的实现方式,并尝试自己修改和扩展。这将是一个实践和巩固...

    精通JavaScript jquery之父力作 附源码

    《精通JavaScript:jQuery之父力作 附源码》是一本深入探讨JavaScript编程技术的书籍,作者在jQuery领域的权威使得这本书具有极高的学习价值。本书旨在帮助读者从基础到高级,全面掌握JavaScript语言,尤其注重实际...

    精通JavaScript源代码

    要精通JavaScript源代码,我们需要深入理解其核心概念,包括变量、数据类型、作用域、函数、闭包、原型链以及面向对象编程。 首先,JavaScript中的变量通过`var`、`let`或`const`关键字声明,具有动态类型,意味着...

    Pro JavaScript Techniques(jQuery之父作品:精通Javascript英文原版+源码)

    8. **源码分析**:随书附带的`code.zip`文件可能包含了书中示例代码,读者可以通过实际运行和分析这些代码,加深对JavaScript编程技巧的理解。 9. **跨浏览器兼容性**:JavaScript在不同浏览器中的行为可能存在差异...

    精通javascript+jqurry源码文件

    在这本“精通JavaScript+jqurry源码文件”的书中,你可能会学习到以下知识点: 1. **JavaScript 基础**:包括变量声明、数据类型、运算符、控制流、函数、对象和数组等基础知识。 2. **JavaScript 高级特性**:如...

    精通JavaScript+jQuery电子书+源码-部分5

    在“精通JavaScript+jQuery电子书+源码-部分5”中,读者可以深入学习这两门技术的高级概念和实践技巧。这部分内容可能涵盖了以下几个关键知识点: 1. **JavaScript高级特性**:这可能包括闭包、原型链、异步编程...

    精通JavaScript动态网页编程(实例版) 光盘源码

    通过"精通JavaScript动态网页编程(实例版)"的学习,读者不仅能够理解这些概念,还能通过光盘源码中的实例进行实践,加深对每个知识点的理解。实例通常涵盖了从基础到高级的应用,帮助读者逐步建立起实际项目中的编程...

    精通JavaScript+jQuery_5+实例源码

    《精通JavaScript+jQuery》是曾顺撰写的一本深入学习JavaScript和jQuery技术的书籍,该书以丰富的实例和源码解析,旨在帮助读者全面掌握这两门重要前端技术。在这个压缩包中,我们找到了“精通JavaScript+jQuery_...

    《精通JavaScript动态网页编程(实例版)》光盘源码

    《精通JavaScript动态网页编程(实例版)》是一本专注于JavaScript技术在动态网页开发中的实践应用的书籍。光盘源码包含了大量的实例,旨在帮助读者深入理解JavaScript如何与AJAX、DHTML、W3C标准以及XML等技术相...

    精通javascript 源代码

    在"精通JavaScript源代码"的学习过程中,你需要掌握以下几个核心知识点: 1. **基础语法**:包括变量声明(var、let、const)、数据类型(原始类型如字符串、数字、布尔、null、undefined,以及对象类型)、操作符...

    精通JavaScript+jquery 源码

    本文将深入探讨这两个技术的核心概念,并结合提供的《精通JavaScript+jQuery》书籍源码来阐述相关知识点。 首先,JavaScript 是一种轻量级的、解释型的编程语言,主要用于增强网页的用户体验。它在浏览器端运行,...

    精通javascript+Jquery源码

    本资源《精通javascript+jQuery源码》旨在帮助学习者深入理解这两门技术的精髓,通过源码分析,加速学习进程。 JavaScript,作为一门客户端脚本语言,是网页动态化的核心。它允许开发者在用户的浏览器上运行代码,...

    JavaScript实例精通_源码实例

    "JavaScript实例精通_源码实例"这个资源集旨在帮助开发者深入理解并熟练掌握JavaScript的核心概念和实际应用。以下是对其中涉及知识点的详细阐述: 1. **基础语法**:JavaScript的基础包括变量、数据类型(如字符串...

Global site tag (gtag.js) - Google Analytics