简体   繁体   English

滚动浏览标题后显示图像 - 尝试不起作用?

[英]Making an image appear after scrolling past header - attempts not working?

I've recently taken over work on a friend's website, here . 我最近接管工作,在一个朋友的网站, 在这里 I want to get the small logo above the description box to only show up once the user has scrolled past (and subsequently hidden) the large header at top, and disappear again if the user scrolls back up past it. 我想让描述框上方的小徽标只在用户滚动过去(并随后隐藏)顶部的大标题后才会显示,如果用户向上滚过它,则会再次消失。 I've tried the methods recommended in these other posts here and here , which seem like the same basic idea but I can't get any of them to work. 我在这里这里尝试了这些其他帖子中推荐的方法,这看起来像是一个基本的想法,但我不能让它们中的任何一个起作用。

I'm new to anything and everything scripting (which I'm entirely sure is the biggest problem here, I know.) So any help is appreciated as what I'm apparently doing wrong. 我对任何东西和所有脚本都是新手(我完全相信这是我在这里遇到的最大问题。)所以任何帮助都会被赞赏,因为我显然做错了。

Start by giving the <div class="fixeddiv"> a style="display: none" . 首先给出<div class="fixeddiv"> a style="display: none" Then add the following (since you're already using jQuery): 然后添加以下内容(因为您已经在使用jQuery):

$(document).ready(function () {
    var contentOffset = getOffset();

    function getOffset() {
        var allOffsets = $("div#content").offset();
        return allOffsets.top;
    }

    $(window).resize(function () {
        contentOffset = getOffset();
    });

    $(window).scroll(function () {
        var windowTop = $(window).scrollTop();

        if (windowTop > contentOffset) {
            $("div.fixeddiv").show();
        } else {
            $("div.fixeddiv").hide();
        }
    });
});

Here's what this code does. 这是这段代码的作用。 When the document is done loading, it gets the number of pixels that the "content" div is from the top of the document (offset). 当文档加载完成时,它获得“内容”div从文档顶部(偏移量)的像素数。 It does this again any time the window is resized. 它会在调整窗口大小时再次执行此操作。 Then, when someone scrolls up or down, it gets the number of pixels that are already hidden above the scroll (scrollTop). 然后,当有人向上或向下滚动时,它会获得已隐藏在滚动上方的像素数(scrollTop)。 If the number of hidden pixels is greater than the offset of the #content div from the top of the window, that means we've scrolled past the top of the content div and should show the icon. 如果隐藏像素的数量大于#content div从窗口顶部的偏移量,则意味着我们滚动到内容div的顶部并应显示图标。 Otherwise, we should hide the icon. 否则,我们应该隐藏图标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM