(窗口).scrollTop函数在第一次单击时不会触发

时间:2014-09-26 14:39:21

标签: javascript jquery

我有一个可点击的条形图,当点击它时,它会触发mainQuestBarClick功能。在这个函数中,我有这一行$(window).scrollTop($('#scrollHere').offset().top);滚动到scrollHere div。当我点击该栏时,它不会在那里滚动,但在第二次点击它时,它会滚动。可能是什么原因?

这是功能:

var mainQuestBarClick = function (event, pos, obj) {
        if (!obj)
            return;
        $(window).scrollTop($('#scrollHere').offset().top);
        //goToByScroll($("#scrollHere").attr("id")); 

        var selectedBranchName = encodeURIComponent(obj.series.label);
        $("#SelectedBranchFromBarChart").val(selectedBranchName);

        $("#content").block();

        //Stats Chart
        $.ajax({
            type: "GET",
            url: '@Url.Action("GetStatsForSpecificBranch", "SurveyReports")',
            data: "BranchName="+encodeURIComponent(obj.series.label)+"&SurveyId=" + $("#SurveyId").val() + "&startDate=" + $("#ReportStartDate").val() + "&endDate=" + $("#ReportEndDate").val(),
            cache: false,
            success: function (r) {
                if (r.success == false) {
                    noty({ text: r.resultText, type: 'error', timeout: 2000, modal: true });
                } else {


                    $("#statboxSurveyCountTitle").html("<b>" + selectedBranchName + "</b> Şubesi Anket Sayısı");
                    $("#statboxSurveyAvgTitle").html("<b>" + selectedBranchName + "</b> Şubesi Anket Ortalaması");
                    $("#statboxSurveyRecommTitle").html("<b>" + selectedBranchName + "</b> Şubesi Tavsiye Oranı")
                    $("#StatboxesDiv").show();
                    $("#SurveyCountVal").text(r.FilledSurveyCount);
                    $("#SurveyAvgVal").text(r.FilledSurveyAverageRating.toFixed(2));
                    $("#SurveyRecommVal").text("%"+r.RecommendYesPercent);
                }
            },
            error: function (error) {
                noty({ text: "Bir hata oluştu lütfen tekrar deneyiniz!", type: 'error', timeout: 2000, modal: true });
            }

        });
$("#content").unblock();

}

1 个答案:

答案 0 :(得分:0)

这是这个问题的后期解决方案,但不知怎的,我设法做了我想做的事情。以下是我解决问题的方法:

我创建了一个函数,它接受要滚动的元素的id:

function goToByScroll(id){
        @{int scroll = 600;
            if(Model.BranchList.Count <= 1)
            {
                scroll = 750;
            }
            }
        // Scroll
        $('html,body').animate({scrollTop:@scroll}, 1000);
    }

并在上面的代码中更改了此部分的问题:

$(window).scrollTop($('#scrollHere').offset().top);

到此:

goToByScroll($("#scrollHere").attr("id"));