简体   繁体   English

根据滚动位置和内容高度切换div可见性

[英]Toggle div visibility according to scroll position and content height

I'm trying to achieve having simple sharing buttons on the side of a post, which are only visible if the post is within the view. 我正在尝试在帖子的侧面实现简单的共享按钮,仅当帖子在视图内时才可见。 So they are hidden if the user is scrolling above or below that post. 因此,如果用户在该帖子的上方或下方滚动,它们将被隐藏。

jQuery waypoint it my current weapon of choice. jQuery Waypoint是我当前选择的武器。 My approach consists of: 我的方法包括:

// first hiding the div
$('.post-sharing-side').hide();

// fading it in as soon as the headline reaches the top of the viewspace (that feels right in my use case)
$('.entry-title').waypoint(function(direction) {
    $('.post-sharing-side').fadeIn();
});

// fading it out again as soon as the upcoming div reaches the bottom of the viewspace
MISSING

I'm having trouble figuring out the last part: fading it out again. 我在弄清楚最后一部分时遇到了麻烦:再次淡出。 Ideally it should also work when scrolling up again. 理想情况下,再次向上滚动时它也应该起作用。 Any ideas would be highly appreciated! 任何想法将不胜感激!

Update: 更新:

Sorry if I wasn't clear enough: the effect I'm looking for is basically this bit without using absolute values. 很抱歉,如果我不够清楚:我要寻找的效果基本上是一点,而没有使用绝对值。

From the waypoint documentation at http://imakewebthings.com/jquery-waypoints/#docs : 从位于http://imakewebthings.com/jquery-waypoints/#docs的Waypoint文档中:

Vertical waypoints may also use the value 'bottom-in-view'. 垂直航点也可以使用值“底部视图”。 This is a shortcut for a common function that sets the waypoint to trigger when the bottom of the element hits the bottom of the viewport. 这是常用功能的快捷方式,该功能可将航路点设置为在元素的底部碰到视口的底部时触发。

$('.thing').waypoint({ offset: 'bottom-in-view' }); $('。thing')。waypoint({offset:'bottom-in-view'});

This might help. 这可能会有所帮助。

I solved it the following way, not using jQuery waypoint at all in the end: 我通过以下方式解决了问题,最终根本没有使用jQuery Waypoint:

$('.post-sharing-side').hide();

var entryheight = $('.entry-content').height();

$(document).scroll(function () {
    var y = $(this).scrollTop();
    if (y > 350 && y < entryheight) {
         $('.post-sharing-side').fadeIn();
    } else {
        $('.post-sharing-side').fadeOut();
    }
});

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

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