js或jquery获取按钮距浏览器顶部距离

js或jquery获取按钮距浏览器顶部距离,第1张

jquery获取元素到页面顶部距离的语句为:

$(selector)offset()top

下面是例子:

<div style="height:800px"> </div>

<div id="footer_keleyi_com" style="text-align:center; margin-left:auto;margin-right:auto; margin-top:20px">柯乐义 Copyright<span style="font-family:Arial;">©</span> <span style="font-size:18px;">keleyicom

</span></div>

那么 $("#footer_keleyi_com")offset()top 就是元素到页面顶部的距离。

使用Jquery 获取DIV相对浏览器的边距,也就是绝对X,Y坐标,可以用offset():

$('div')offset()left;

示例如下:

创建Html元素

<div class="top">

本层相对浏览器的左边距为50px

<div class="inner">本层相对上一层的左边距为20px,所以相对浏览器边距为70px</div>

</div>

设置css样式

{margin:0;}

divtop{

width:500px;height:150px;

margin:50px;

background:green;

color:white;

}

divinner{

width:450px;height:100px;line-height:100px;

margin:20px;

background:red;

color:white;

}

编写jquery代码

$(function(){

$("divinner")click(function() {

alert("相对窗口左边距:"+$(this)offset()left);

});

})

观察效果

/

 锚点点击跳转

 /

var AnchorClick = function (pos) {

    $("html,body")animate({ scrollTop: pos }, speed);

};

pos为你要跳转到位置:也就是你要跳转的位置举例浏览器顶部的距离,单位为px

以上就是关于js或jquery获取按钮距浏览器顶部距离全部的内容,包括:js或jquery获取按钮距浏览器顶部距离、Jquery 如何获得DIV离左边是多少像素、怎样才能让页面滚动直接滚到动一定的距离 jquery等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9284692.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存