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

前端 未结 7 2000
无人共我
无人共我 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: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 = $('').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.

提交回复
热议问题