zoukankan      html  css  js  c++  java
  • JQuery 获得绝对,相对位置的坐标方法

    获取页面某一元素的绝对X,Y坐标,可以用offset()方法:(body属性设置margin :0;padding:0;)

    var X = $('#DivID').offset().top;
    var Y = $('#DivID').offset().left;
    获取相对(父元素)位置:
    var X = $('#DivID').position().top;
    var Y = $('#DivID').position().left;

    复制代码 代码如下:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>java Test</title>
    </head>
    <style type="text/css">
    <!--
    body,div { margin:0; padding:0;}
    -->
    </style>
    <script type="text/javascript" src="js/jquery.js"></script>

    <body>
    <div style="background:#ccc;height:300px;" onclick=""></div>
    <div style="position:relative;">
    <div style=" position:absolute;left:50px; top:50px;" id="DivID"></div>
    </div>
    <script type="text/javascript">
    var X = $('#DivID').offset().top;
    var Y = $('#DivID').offset().left;
    document.write(X+"<br />");
    document.write(Y+"<br />");
    //获取相对(父元素)位置:
    var C = $('#DivID').position().top;
    var D = $('#DivID').position().left;
    document.write(C+"<br />");
    document.write(D);
    </script>
    </body>
    </html> 
  • 相关阅读:
    CodeForces
    CodeForces
    CodeForces 718C && HDU 3572 && Constellation
    CodeForces
    USACO 5.4 tour的dp解法
    10.22~10.28一周经典题目整理(meeting,BZOJ4377,POJ3659)
    codeforces 724D
    codeforces 724C
    hdu5909 Tree Cutting
    hdu5822 color
  • 原文地址:https://www.cnblogs.com/telwanggs/p/7723814.html
Copyright © 2011-2022 走看看