2014-09-26 98 views
1

我有一個可點擊的條形圖,當它被點擊時,它會觸發mainQuestBarClick函數。在這個函數中,我有這條線$(window).scrollTop($('#scrollHere').offset().top);滾動到scrollHere div。當我點擊欄時,它不會在那裏滾動,但第二次點擊它時,它會滾動。可能是什麼原因?(窗口).scrollTop函數在第一次點擊時不會觸發

下面是函數:

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

很好,我們需要看到更多的代碼的確切地告訴你的原因。 – bipen 2014-09-26 14:40:30

+0

謝謝,我更新了代碼。 – 2014-09-26 14:44:51

+0

如果你嘗試'$(window).scrollTop(0);'? – 2014-09-26 14:50:25

回答

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")); 
相關問題