子标题内的平滑滚动元素位于标题下方(Smooth scroll elements inside child div which is below header)

编程入门 行业动态 更新时间:2024-10-24 14:20:04
标题内的平滑滚动元素位于标题下方(Smooth scroll elements inside child div which is below header)

所以这里是小提琴链接https://jsfiddle.net/malakar369/y5Lppeg0/8/

jQuery(document).ready(function ($) { $('.custom-click').on('click', function (e) { e.preventDefault(); var href = $(this).attr('href'); var current_div = $(href); var animateTo = $(href).position().top ; console.log(animateTo); $('#main-height-container').animate({scrollTop: animateTo},"slow"); }); });

如果我的div位于页面顶部,它就可以工作。 但是,由于它不是最重要的,我有大量的问题。 你们中的任何人都可以看看它并帮助我。

谢谢

So here is the fiddle link https://jsfiddle.net/malakar369/y5Lppeg0/8/

jQuery(document).ready(function ($) { $('.custom-click').on('click', function (e) { e.preventDefault(); var href = $(this).attr('href'); var current_div = $(href); var animateTo = $(href).position().top ; console.log(animateTo); $('#main-height-container').animate({scrollTop: animateTo},"slow"); }); });

It works if my div is at the top of the page. However, since it is not at the top i am having and extensive amount of issue. Could any of you guys could have a look at it and help me out.

Thanks

最满意答案

$(href).position()。top在窗口坐标中

尝试这个

var animateTo = $("#main-height-container").scrollTop() - $("#main-height-container").position().top + $(href).position().top;

https://jsfiddle.net/y5Lppeg0/9/

$(href).position().top is in window coordinates

Try this

var animateTo = $("#main-height-container").scrollTop() - $("#main-height-container").position().top + $(href).position().top;

https://jsfiddle.net/y5Lppeg0/9/

更多推荐

本文发布于:2023-07-15 21:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119337.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标题   平滑   元素   Smooth   scroll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!