Magnific Popup自动滚动正文内容

时间:2015-03-06 12:17:34

标签: jquery magnific-popup

我在此Magnific Popup上安装了website

每次单击缩略图以启动灯箱时,背景中的正文内容会略微向下滚动。有没有办法阻止这种情况发生?

我有一种感觉,灯箱脚本在某种程度上干扰了我滚动到顶部代码:

    // Magnific Popup open inline content
    $('.open-popup-link').magnificPopup({
        type:'inline',
        midClick: true
    });

    // Magnific Popup open iframe content
    $('.vimeolink').magnificPopup({
        type: 'iframe',
    });

    // Smooth scrolling - https://css-tricks.com/snippets/jquery/smooth-scrolling/
    $('a[href*=#]:not([href=#])').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
            if (target.length) {
                $('html,body').animate({
                    scrollTop: target.offset().top
                }, 1000);
                return false;
            }
        }
    });

    // Scroll to top - http://www.paulund.co.uk/how-to-create-an-animated-scroll-to-top-with-jquery
    $(window).scroll(function(){
        if ($(this).scrollTop() > 100) {
            $('.scrollToTop').fadeIn();
        } else {
            $('.scrollToTop').fadeOut();
        }
    });

    // Click event to scroll to top
    $('.scrollToTop').click(function(){
        $('html, body').animate({scrollTop : 0},800);
        return false;
    });

2 个答案:

答案 0 :(得分:1)

$('a[href*=#]:not([href=#]')匹配点击的图片,例如<a class="open-popup-link" href="#popup-183">。所以是的,当你点击一个图像时,不仅弹出窗口显示,而且页面滚动到URL中的哈希值,在这种情况下a#projects

您可以通过添加类来区分<a>的锚点和<a>的图像,例如:

<a class="scroller" href="#whatever"></a>

<a class="open-popup-link" href="#popup-183">

现在使用他们的类来定位他们:

$('a.scroller').click(function() { /* scrolling stuff */ }

答案 1 :(得分:1)

您也可以尝试使用Featherlight。此插件不会在链接中使用href属性来显示内容,因此您不会遇到该问题。它也更容易使用,而且他们的文档更好(IMO)。