1

我编写了自己的函数,该函数在滚动某个点后将侧边栏固定在屏幕上,然后在滚动回顶部时将其返回到它的相对位置。如果窗口的大小调整为小于侧边栏高度的大小,它也会将其返回到其正常的相对位置。效果很好!

问题是当我在页面主体的全景缩略图上运行另一个函数,即 fancybox() 并尝试滚动时,我原来的“滚动修复”函数似乎停止工作。

有谁知道这是为什么?

//////////////////
演示页面
/////////////////


//////////////////////////////
“滚动修复”功能

 $(document).ready(function(){

  var element = $("#sidebar");
  var window_height = $(window).height();
  var element_height = element.height();

  $(window).ready(function() { 
    if (window_height > element_height) {
      if (($(document).scrollTop() + element.height()) > ($(document).height() - $("#footer").height() - 9)) {
        element.css("position","absolute");
        element.css("top", "auto");
        element.css("bottom","-60px");
      }
      else if ($(document).scrollTop() > 220) {
        element.css("position","fixed");
        element.css("top","9px");
        element.css("padding-top","0");
        element.css("bottom","auto");
      }
      else {
        element.css("position","relative");
        element.css("top","auto");
        element.css("padding-top","57px");
        element.css("bottom","auto");    
      }
    }
    else {
      element.css("position","relative");
      element.css("top","auto");
      element.css("padding-top","57px");
        element.css("bottom","auto");
    }
  });


  $(window).scroll(function() { 
    if (window_height > element_height) {
      if (($(document).scrollTop() + element.height()) > ($(document).height() - $("#footer").height() - 9)) {
        element.css("position","absolute");
        element.css("top", "auto");
        element.css("bottom","-60px");
      }
      else if ($(document).scrollTop() > 220) {
        element.css("position","fixed");
        element.css("top","9px");
        element.css("padding-top","0");
        element.css("bottom","auto");
      }
      else {
        element.css("position","relative");
        element.css("top","auto");
        element.css("padding-top","57px");
        element.css("bottom","auto");    
      }
    }
    else {
      element.css("position","relative");
      element.css("top","auto");
      element.css("padding-top","57px");
        element.css("bottom","auto");
    }
  });

  $(window).resize(function(){
    window_height = $(window).height();
    if (window_height > element_height) {
      if (($(document).scrollTop() + element.height()) > ($(document).height() - $("#footer").height() - 9)) {
        element.css("position","absolute");
        element.css("top", "auto");
        element.css("bottom","-60px");
      }
      else if ($(document).scrollTop() > 220) {
        element.css("position","fixed");
        element.css("top","9px");
        element.css("padding-top","0");
        element.css("bottom","auto");
      }
      else {
        element.css("position","relative");
        element.css("top","auto");
        element.css("padding-top","57px"); 
        element.css("bottom","auto");     
      }
    }
    else {
      element.css("position","relative");
      element.css("top","auto");
      element.css("padding-top","57px");
        element.css("bottom","auto");
    }
  });

});
4

3 回答 3

3

我有一个非常相似的问题,我尝试评论它并且它有效,但之后我尝试了另一种解决方案并且它也有效。基本上,我只针对需要关闭滚动的实际元素,在我的情况下是 prettyPhoto

$(window).unbind('scroll', $.prettyPhoto.close);

粗体部分$.prettyPhoto.close是我添加的。

我希望这可以帮助任何遇到漂亮照片滚动关闭问题的人。

于 2010-10-18T06:07:17.743 回答
0

问题解决了。

原来 FancyBox.js 有一行基本上是说,当你关闭花哨的盒子时......

if (opts.centerOnScroll) {
  $(window).unbind("resize scroll");
}

取消绑定窗口会导致我的滚动修复解决方案也取消绑定。

对该行的简单注释(在该 fancybox 关闭函数中出现两次)解决了这个问题。

if (opts.centerOnScroll) {
//  $(window).unbind("resize scroll");
}
于 2009-09-09T21:31:02.477 回答
0

此代码的第一次更改不会产生相同的问题

(function($) {
    $.fn.myScrollFix = function(options) {

        options = $.extend({
            footer:  "footer",
            pthis: this,
            doc: $(document),
            win: $(window)
        }, options || {});

        options.footer = $(options.footer);
        options.accion = function() {

            var element = options.pthis,
                doc_scroll_top = options.doc.scrollTop(),
                doc_height  = options.doc.height(),
                window_height = options.win.height(),
                element_height = options.pthis.height(),
                footer_height = options.footer.height();

            if (window_height > element_height) {
                if ((doc_scroll_top + element_height) > (doc_height - footer_height - 9)) {
                    element
                        .css("position","absolute")
                        .css("top", "auto")
                        .css("bottom","-60px");
                }
                else if (doc_scroll_top > 220) {
                    element
                        .css("position","fixed")
                        .css("top","9px")
                        .css("padding-top","0")
                        .css("bottom","auto");
                }
                else {
                    element
                        .css("position","relative")
                        .css("top","auto")
                        .css("padding-top","57px")
                        .css("bottom","auto");    
                }
            }
            else {
                element
                    .css("position","relative")
                    .css("top","auto")
                    .css("padding-top","57px")
                    .css("bottom","auto");
            }
        };

        $(window).bind("scroll", options.accion);
        $(window).bind("resize", options.accion);

        options.accion();
    };
})(jQuery);
$(document).ready(function(){
    $("#sidebar").myScrollFix();
});

然后您可以在 FancyBox 中修改这些行

第 432 行

$(window).unbind("resize scroll", $.fn.fancybox.scrollBox);

第 439 行

$(window).unbind("resize scroll", $.fn.fancybox.scrollBox);
于 2009-09-09T21:40:21.063 回答