简体   繁体   English

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

[英](window).scrollTop function doesn't fire at first click

I have a bar chart which is clickable, and when its clicked, it fires mainQuestBarClick function. 我有一个可单击的条形图,当它被单击时,它会触发mainQuestBarClick函数。 In this function, I have this line $(window).scrollTop($('#scrollHere').offset().top); 在此函数中,我有这行$(window).scrollTop($('#scrollHere').offset().top); to scroll to scrollHere div. 滚动到滚动此处div。 When I click to the bar, it doesn't scroll there but at second time I click it, it scrolls. 当我单击该栏时,它不会在那里滚动,但是在第二次单击时,它会滚动。 What could be the reason? 可能是什么原因?

Here is the function: 这是函数:

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();

}

This is a late solution to this problem but somehow I managed to do what I want to do. 这是解决此问题的较晚方法,但是我设法做到了自己想做的。 Here is how I solved the problem: 这是我解决问题的方法:

I created a function which takes the id of the element to be scrolled: 我创建了一个函数,该函数采用要滚动的元素的id:

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

And changed this part in my code above in the question: 并在上面的问题中更改了我的代码的这一部分:

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

to this: 对此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM