【问题标题】:Dynamically making an element fixed (header) in fullpage.js在 fullpage.js 中动态固定元素(标题)
【发布时间】:2015-01-16 20:13:18
【问题描述】:

我正在fullpage.js 中建立一个页面。第一张幻灯片上的图像占用了视口高度的 90%。另外 10% 是图片下方的导航栏。下图展示了这一点。

当我滚动到下一张幻灯片时,我希望导航栏成为其余幻灯片的固定标题。

我尝试使用 jQuery 修复 offset().top 值对 $(window).top() 的 0 的元素。这对我不起作用。

$(window).scroll(function () {
    var nav = $('#nav');

    var eTop = nav.offset().top;
    if ((eTop - $(window).scrollTop()) == 0) {
        nav.addClass('fixed');
    }
    else {
        nav.removeClass('fixed');
    }
});

这可能吗?我该如何实现?

【问题讨论】:

标签: javascript jquery html css fullpage.js


【解决方案1】:

如果您使用默认选项css3:true,那么这将解决问题:

$('#fullpage').fullpage({
    sectionsColor: ['yellow', 'orange', '#C0C0C0', '#ADD8E6'],
    onLeave: function(index, nextIndex, direction){
        //leaving 1st section
        if(index == 1){
           $('.header').addClass('fixed');
        }
        //back to the 1st section
        if(nextIndex == 1){
            $('.header').removeClass('fixed');
        }
    }      
});

并且您将需要此 CSS 作为标题元素:

.header{
    -webkit-transition: all 0.7s ease;  
    -moz-transition: all 0.7s ease;  
      -o-transition: all 0.7s ease; 
         transition: all 0.7s ease; 

    position:absolute;
    top:100%;
    margin-top: -100px;
    left:0;
    background:#000;
    width:100%;
    color: #fff;
    height: 100px;
    z-index:999;
}
.header.fixed{
    bottom:auto;
    top:0;
    margin-top: 0;
}

你当然可以,改变高度等等。

考虑到我已将固定元素放置在插件的包装器之外。这样我就可以避免插件使用的translate3d属性出现问题:

<div class="header">Header</div>

<div id="fullpage">
    <div class="section">...</div>
    <div class="section">...</div>
   ...
</div>

See a demo

更新:

如果您使用的是scrollBar:true,请使用以下 CSS 代替之前的 CSS:

.section {
    text-align:center;
}
.header{
    -webkit-transition: all 0.7s cubic-bezier(0.895, 0.030, 0.685, 0.220);  
    -moz-transition: all 0.7s cubic-bezier(0.895, 0.030, 0.685, 0.220);  
      -o-transition: all 0.7s cubic-bezier(0.895, 0.030, 0.685, 0.220); 
         transition: all 0.7s cubic-bezier(0.895, 0.030, 0.685, 0.220); 
    position:fixed;
    top:100%;
    margin-top: -100px;
    left:0;
    background:#000;
    width:100%;
    color: #fff;
    height: 100px;
    z-index:999;
}
.header.fixed{
    bottom:auto;
    top:0;
    margin-top: 0;
    position:fixed;
}

See demo

【讨论】:

  • 这很酷@Alvaro。但是当我转scrollBar: true 时它不起作用。
【解决方案2】:

为什么不只检查您是否滚动超过了窗口的高度?

Check out my fiddle here

$(window).scroll(function () {
var nav = $('#nav');

var offset = $(this).height();
if (($(window).scrollTop()) >= offset) {
    nav.addClass('fixed');
}
else {
    nav.removeClass('fixed');
}
});

【讨论】:

  • fullPage.js 不滚动站点。它会更改包装器的 toptranslate3d 属性。所以滚动事件不会对其产生任何影响。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
  • 2014-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多