Scrolltop偏移量最大的问题

编程入门 行业动态 更新时间:2024-10-19 10:21:34
本文介绍了Scrolltop偏移量最大的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用下面提到的代码滚动到div.我在移动视图中应用了scrollTop: $(target).offset().top-250,以便该部分显示在标题高度以下,但似乎不起作用.

I used the below mentioned code for scrolling to a div. I applied scrollTop: $(target).offset().top-250 in the mobile view so that the section will display below the header height, but it seems it does not work.

$('#portfolioNavbar ul li a[href*=#]').bind('click', function(e) { e.preventDefault(); var target = $(this).attr("href"); $('html, body').stop().animate({ scrollTop: $(target).offset().top - 250 }, 600, function() { location.hash = target; //attach the hash (#jumptarget) to the pageurl }); return false; });

推荐答案

以下脚本节省了我的时间.该脚本可能对其他人有用.

The below script saved my time. This script may be useful for others.

$('#portfolioNavbar ul li a').click(function() { var tghsh = $(this).attr('href').substring(1); var headerHeight = $('.portfolioi_left').outerHeight(); var winwid = $(window).width(); var doffset = $('#'+tghsh).offset().top-20; var doffset1 = $('#'+tghsh).offset().top-280; if(winwid <= 991) { doffset1 -= headerHeight; $('html, body').animate({scrollTop:doffset1},700); return false; } $('html, body').animate({scrollTop:doffset},700); return false; });

如果要使菜单在滚动到div时处于活动状态.这个脚本可以吗

If want to make menu active on the scroll to div. can you this script

$(document).ready(function() { $('#portfolioNavbar ul li a').click(function() { $(document).off("scroll"); $('#portfolioNavbar ul li a').each(function () { $(this).removeClass('active'); }) $(this).addClass('active'); var tghsh = $(this).attr('href').substring(1); var headerHeight = $('.portfolioi_left').outerHeight(); var winwid = $(window).width(); var doffset = $('#'+tghsh).offset().top-20; var doffset1 = $('#'+tghsh).offset().top-280; if(winwid <= 991) { doffset1 -= headerHeight; $('html, body').animate({scrollTop:doffset1},700); return false; } $('html, body').animate({scrollTop:doffset},700); return false; }); }); function onScroll(event){ var scrollPos = $(document).scrollTop(); $('#portfolioNavbar ul li a').each(function () { var currLink = $(this); var refElement = $(currLink.attr("href")); if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) { $('#portfolioNavbar ul li a').removeClass("active"); currLink.addClass("active"); } else{ currLink.removeClass("active"); } }); };

更多推荐

Scrolltop偏移量最大的问题

本文发布于:2023-11-12 04:04:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580469.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:偏移量   Scrolltop

发布评论

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

>www.elefans.com

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