Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent page scroll after click?

After clicking on the link, Click Me, the page scrolls back to the top. I do not want this. How can fix it?

Example: http://jsfiddle.net/Y6Y3Z/

Scroll-bar:

enter image description here

    function myalert() {
        var result = true;
        //var hide = $('.alert').fadeOut(100);
        //var css = $('#appriseOverlay').css('display','none');
        var $alertDiv = $('<div class="alert">Are you sure you want to delete this item?<div class="clear"></div><button class="ok">no</button><button class="cancel">yes</button></div>');
        var link = this;
        $('body').append($alertDiv);
        $('.ok').click(function () {
            $('.alert').fadeOut(100);
            $('#appriseOverlay').css('display', 'none');
            callback(true, link);
        });
        $('.cancel').click(function () {
            $('.alert').fadeOut(100);
            $('#appriseOverlay').css('display', 'none');
            callback(false, link);
        });
        $alertDiv.fadeIn(100);
        $('#appriseOverlay').css('display', 'block');
        return result;
    };
$('.click').click(myalert);
    function callback(result, link) {
        //
        if(result){
        }
    }
like image 328
Jennifer Anthony Avatar asked Oct 08 '11 15:10

Jennifer Anthony


People also ask

How do I stop a page from scrolling?

Disabling scroll with only CSS. There's another way to disable scrolling that is commonly used when opening modals or scrollable floating elements. And it is simply by adding the CSS property overflow: hidden; on the element you want to prevent the scroll.

How do I stop background scrolling from popping up when open?

Approach: A simple solution to this problem is to set the value of the “overflow” property of the body element to “hidden” whenever the modal is opened, which disables the scroll on the selected element.


1 Answers

You only need to change the "#" to "javascript:void(0)" on HTML code:

<a href="javascript:void(0)" class="click">Click Me</a>

Another solution is add a "/" after the "#":

<a href="#/" class="click">Click Me</a>
like image 160
Gilberto Sánchez Avatar answered Oct 30 '22 00:10

Gilberto Sánchez