1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; } body{ height: 2000px; } #root{ width: 300px; height: 100px; background-color: sandybrown; margin: 50px; position: relative; overflow: hidden; } #div1{ width: 200px; height: 200px; background-color: yellowgreen; position: absolute; left: 30px; top: 40px; padding: 20px; border:5px solid skyblue; margin: 10px; } </style> </head> <body> <div id="root"> <div id="div1">jq中跟位置相关的属性</div> <img src="img/t1.png"/> </div> </body> </html> <script src="js/jquery-3.5.1.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript">
console.log($("#div1").width()); console.log($("#div1").innerWidth()); console.log($("#div1").outerWidth()); console.log($("#div1").outerWidth(true)); $("#div1").width(100);
console.log("div1距离窗口左边", $("#div1").offset().left); console.log("div1距离窗口上面", $("#div1").offset().top); $("#root").offset({ left:100, top:100 }); console.log($("#div1").position()); console.log($("#div1").position().left); console.log($("#div1").position().top); $("#root").scrollLeft(500); $("#root").scrollTop(300); </script>
|