简体   繁体   中英

Expand/Collapse brings page to top

If element is expanded or collapsed whenever you are scrolled down the page, the page is abruptly reset and focus is moved to the top of the page.

Any way to prevent the page from shifting or make this less abrupt?

    $(document).ready(function () {
        $(".flip").click(function () {
        var panel = $(this).next('.panel');
        $('.panel').not(panel).slideUp('slow');
        $(this).next('.panel').slideToggle("slow");
       });
    });

http://jsfiddle.net/chaddly/5FNTX/

Try This add "e.preventDefault();"

$(document).ready(function () {
    $(".flip").click(function (e) {
        e.preventDefault();
        var panel = $(this).next('.panel');
        $('.panel').not(panel).slideUp('slow');
        $(this).next('.panel').slideToggle("slow");
    });
});

Because the element you are clicking on contains a link with # it is navigating to that which is causing it to move to the top of the page.

Add a call to preventDefault() will stop this:

$(".flip").click(function (e) {
     e.preventDefault();
     var panel = $(this).next('.panel');
     $('.panel').not(panel).slideUp('slow');
     $(this).next('.panel').slideToggle("slow");
});

http://jsfiddle.net/5FNTX/1/

you can put return false after last toggle.

$(document).ready(function () {
 $(".flip").click(function () {
  var panel = $(this).next('.panel');
  $('.panel').not(panel).slideUp('slow');
  $(this).next('.panel').slideToggle("slow");
  return false;
 });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM