如何修复与滚动相关的jQuery冲突?

时间:2016-05-12 13:37:04

标签: jquery

另一个问题是,我遇到了一个jQuery冲突,阻止了我的“滚动到顶部”代码的工作。

我正在使用Wordpress在无冲突模式下加载jQuery 1.12.3。

我有以下代码,用于从我的帖子标题和图片向下滚动到实际内容。

// Smooth scroll to content
var $ = jQuery.noConflict();

$(document).ready(function(){
    $('a[href^="#"]').on('click',function (e) {
        e.preventDefault();

        var target = this.hash;
        var $target = $(target);

        $('html, body').stop().animate({
            'scrollTop': $target.offset().top - 50
        }, 900, 'swing', function () {
            window.location.hash = target;
        });
    });
});

然后在同一页面上,我有以下代码,允许用户滚动到顶部。

jQuery(document).ready(function($){ 
    $(window).scroll(function(){ 
        if ($(this).scrollTop() > 100) { 
            $('.scrollup').fadeIn(); 
        } else { 
            $('.scrollup').fadeOut(); 
        } 
    }); 
    $('.scrollup').click(function(){ 
        $("html, body").animate({ scrollTop: 0 }, 600); 
        return false; 
    }); 
});

现在,如果我删除顶部的内容代码滚动,那么滚动到顶部代码工作正常,但是它们都出现在页面上,只有前者有效。

当页面上出现“滚动到顶部”代码时,浏览器会显示错误TypeError: undefined is not an object (evaluating '$target.offset().top')

0 个答案:

没有答案
相关问题