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

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

提交回复
热议问题