How to maintain page scroll position after a jquery event is carried out?

前端 未结 7 1968
无人共我
无人共我 2020-12-04 23:47

I have searched high and low for an answer and have found similar examples of the problem but the answers do not apply to my scenario. The reality is I am new to this and th

相关标签:
7条回答
  • 2020-12-05 00:28

    What you want to do is prevent the default action of the click event. To do this, you will need to modify your script like this:

    $(document).ready(function() {
      $('.galleryicon').live("click", function(e) {
    
        $('#mainImage').hide();
        $('#cakebox').css('background-image', "url('ajax-loader.gif')");
        var i = $('<img />').attr('src',this.href).load(function() {
            $('#mainImage').attr('src', i.attr('src'));
            $('#cakebox').css('background-image', 'none');
            $('#mainImage').fadeIn();
        });
        return false; 
        e.preventDefault();
       });
     });
    

    So, you're adding an "e" that represents the event in the line $('.galleryicon').live("click", function(e) { and you're adding the line e.preventDefault();

    0 讨论(0)
  • 2020-12-05 00:29

    For all who came here from google and are using an anchor element for firing the event, please make sure to void the click likewise:

    <a  
    href='javascript:void(0)'  
    onclick='javascript:whatever causing the page to scroll to the top'  
    ></a>
    
    0 讨论(0)
  • 2020-12-05 00:38
    $('html,body').animate({
      scrollTop: $('#answer-<%= @answer.id %>').offset().top - 50
    }, 700);
    
    0 讨论(0)
  • 2020-12-05 00:40

    Something to note, when setting the scroll position, make sure you do it in the correct scope. For example, if you're using the scroll position in multiple functions, you would want to set it outside of these functions.

    $(document).ready(function() {
        var tempScrollTop = $(window).scrollTop();
        function example() {
            console.log(tempScrollTop);
        };
    
    });
    
    0 讨论(0)
  • 2020-12-05 00:41

    You can save the current scroll amount and then set it later:

    var tempScrollTop = $(window).scrollTop();
    
    ..//Your code
    
    $(window).scrollTop(tempScrollTop);
    
    0 讨论(0)
  • 2020-12-05 00:43

    Try the code below to prevent the default behaviour scrolling back to the top of the page

    $(document).ready(function() {   
      $('.galleryicon').live("click", function(e) {  // the (e) represent the event
        $('#mainImage').hide();     
        $('#cakebox').css('background-image', "url('ajax-loader.gif')");     
        var i = $('<img />').attr('src',this.href).load(function() {         
          $('#mainImage').attr('src', i.attr('src'));         
          $('#cakebox').css('background-image', 'none');         
          $('#mainImage').fadeIn();     
        });
      e.preventDefault(); //Prevent default click action which is causing the 
      return false;       //page to scroll back to the top
      });  
    });
    

    For more information on event.preventDefault() have a look here at the official documentation.

    0 讨论(0)
提交回复
热议问题