无法阅读物业' top'单击按钮打开模态窗口时发生

时间:2015-04-19 11:23:09

标签: javascript jquery bootstrap-modal

以下代码在页面加载时有效。但是当我点击一个应该打开模态窗口的按钮时,我收到以下错误:

未捕获的TypeError:无法读取属性' top'未定义的

如何解决此错误?

if ($(window).width() > 992) {

    (function($) {
        "use strict";
        $(".page-scroll a[href^='#'], #intro a").on('click', function(e) {
            e.preventDefault();
            var hash = this.hash;
            $('html, body').stop().animate({
                scrollTop: $(hash).offset().top - 100}, 2000, 'easeOutExpo');
        });
    })(jQuery);

    $('body').scrollspy({
        target: '.navbar',
        offset: 110
    });
    // alert("large");
}
else {

    (function($) {
        "use strict";
        $(".page-scroll a[href^='#'], #intro a").on('click', function(e) {
            e.preventDefault();
            var hash = this.hash;
            $('html, body').stop().animate({
                scrollTop: $(hash).offset().top - 50}, 2000, 'easeOutExpo');
        });
    })(jQuery);

    $('body').scrollspy({
        target: '.navbar',
        offset: 70
    });
    // alert("small");
}   

//modal popup function
function popup_modal(item){
    var link = $(item).attr('id');
    $('#bootstrap_modal').load('/'+link+'');
    $('#bootstrap_modal').modal('show'); 
    $("#bootstrap_modal").on("show", function () {
        $("body").addClass("modal-open");
    }).on("hidden", function () {
        $("body").removeClass("modal-open")
    });
}

//Modal pop up
$('.mmodal').on('click', function(){
    popup_modal(this);
});

2 个答案:

答案 0 :(得分:1)

您收到错误的可能原因是,对于模态按钮,$(hash)元素不存在。模态按钮是属于$(".page-scroll a[href^='#'], #intro a").on('click')事件的元素。如果没有id等于" href"点击按钮的属性,你无法得到它" offset.top"。 把" console.log(hash)"检查你到那里。

可能的解决方案:

if ($(hash).length) {
    $('html, body').stop().animate({
         scrollTop: $(hash).offset().top - 100}, 2000, 'easeOutExpo');
}

答案 1 :(得分:0)

尝试以下代码:

if ($(window).width() > 992) {

    (function($) {
        "use strict";
        $(".page-scroll a[href^='#'], #intro a").on('click', function(e) {
            e.preventDefault();
            var hash = this.hash;
            var newVal = $(hash).offset().top - 100;
            $('html, body').stop().animate({
                scrollTop: newVal}, 2000, 'easeOutExpo');
        });
    })(jQuery);

    $('body').scrollspy({
        target: '.navbar',
        offset: 110
    });
    // alert("large");
}
else {

    (function($) {
        "use strict";
        $(".page-scroll a[href^='#'], #intro a").on('click', function(e) {
            e.preventDefault();
            var hash = this.hash;
            var newVal = $(hash).offset().top - 50;
            $('html, body').stop().animate({
                scrollTop: newVal}, 2000, 'easeOutExpo');
        });
    })(jQuery);

    $('body').scrollspy({
        target: '.navbar',
        offset: 70
    });
    // alert("small");
}   

//modal popup function
function popup_modal(item){
    var link = $(item).attr('id');
    $('#bootstrap_modal').load('/'+link+'');
    $('#bootstrap_modal').modal('show'); 
    $("#bootstrap_modal").on("show", function () {
        $("body").addClass("modal-open");
    }).on("hidden", function () {
        $("body").removeClass("modal-open")
    });
}

//Modal pop up
$('.mmodal').on('click', function(){
    popup_modal(this);
});

我已将变量newVal设置为$(hash).offset().top - 50

的值