简体   繁体   English

JavaScript生成的margin-top搞乱了展示位置的内容

[英]Javascript generated margin-top messes up placement content

Case: 案件:

I have a slider that scrolls through some images, while, when you scroll down, the menu and content moves over it. 我有一个滑块,可以滚动浏览某些图像,而当您向下滚动时,菜单和内容将在其上移动。 Once the menu gets to the top, it sticks to it, as it's changed to a fixed position. 菜单移至顶部后,将其固定在菜单上,因为它已更改为固定位置。

Problem: 问题:

Once the menu snaps to place, it resets it's original position (a margin-top) from a generated ammount of pixels to the 0 value. 菜单捕捉到位置后,它将其原始位置(生成的像素数量)重置为0值(原始边缘)。 This cases the page to jump that generated ammount of pixels down, which shouldn't happen. 这会导致页面跳转而产生的像素下降,这种情况不应该发生。 It shouldn't jump down at all, but I think it has to do with the ammount of pixels that is generated before it is set to 0, which cases on my screen a gap of 955 pixels. 它根本不应该跳下来,但是我认为这与在将其设置为0之前生成的像素数量有关,在我的屏幕上,这相当于955像素。 It jumps thus 955 pixels down after it applies the fixed state. 因此,在应用固定状态后,它会向下跳955个像素。

So my questio now is, how can i fix this. 所以我现在的问题是,我该如何解决这个问题。 I tried applying instead of a margin a padding (no go, white screen), applying instead of a margin-top: 0px a top:0 so i dont have to use the margins, but also a no go. 我尝试应用填充而不是空白(不走,白屏),而不是使用margin-top:0px a top:0,所以我不必使用边距,但是也不能。

Case link: 案例链接:

http://test.thewebfanatics.com/jellyweb/home http://test.thewebfanatics.com/jellyweb/home

Code

$(window).scroll(function () {
    if ($('.resolutionwrap').length == 1) {
        var documentScrollTop = $(document).scrollTop() + 100;
        var fixedToggle = $('#slides').height();

        if (documentScrollTop > fixedToggle) {
            $('#hoofdmenu').addClass('fixed');
            $('#hoofdmenu').css("margin-top", "0px");
        } else {
            $('#hoofdmenu').removeClass('fixed');
            $('#hoofdmenu').css("margin-top", $('#slides').height() - 100); 
        }
    }
});

Hope someone can help me on this matter. 希望有人可以帮助我。

Okay, as I was posting the remark on the fiddle, I realized that if the content moved, I could also just simply return it to it's position by javascript by making a counter balanced value of it. 好的,当我在小提琴上发表评论时,我意识到,如果内容移动了,我还可以通过使用javascript平衡值来简单地将其返回到它的位置。

Shortly said: I countered the margin, by creating a different margin-top that balanced that scale. 简而言之:我通过创建一个可以平衡该比例的不同边际利润来抵消利润。 It's maybe not the most beautifull solution, but it did the trick. 这可能不是最美丽的解决方案,但确实成功了。

$(window).scroll(function () {
    if ($('.resolutionwrap').length == 1) {
        var documentScrollTop = $(document).scrollTop() + 100;
        var fixedToggle = $('#slides').height();
//      console.log($('#slides').height());
//      console.log($('.resolutionwrap').height());

        if (documentScrollTop > fixedToggle) {
            $('#hoofdmenu').addClass('fixed');
            $('#hoofdmenu').css("margin-top", "0px");
            $('.content').css("margin-top", $('#slides').height());  
        } else {
            $('#hoofdmenu').removeClass('fixed');
            $('#hoofdmenu').css("margin-top", $('#slides').height() - 100); 
            $('.content').css("margin-top", "0px"); 
        }
    }
})

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

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