【问题标题】:How to set a maximum top margin for a fixed element?如何为固定元素设置最大上边距?
【发布时间】:2015-01-11 18:04:03
【问题描述】:

所以我有这个代码:

jQuery(document).ready(function($){ 
    $( window ).scroll(function() {
        var current_top = $(document).scrollTop(); 
        var heights = window.innerHeight;
        document.getElementById("sHome").style.height = heights * 0.8 + "px";
        $('#sHome').css('top', (current_top*1));    

    });

});

更准确地说,我指的是:

$('#sHome').css('top', (current_top*1));

设置上边距等于滚动。

它工作正常,但是当我使用检查元素时,我有一个无限滚动,所以它就像 top: 7000px;

有没有办法限制这个值?

我找到了一个函数,Math.max(),但我不知道它在我的示例中是否有效。

干杯。

【问题讨论】:

    标签: jquery css scroll fixed


    【解决方案1】:
    $('#sHome').css('top', Math.min((current_top*1), YOUR_MAXIMUM_VALUE));
    

    应该没问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 2015-01-08
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多