Bootstrap Carousel Jumps to top of page when advancing

前端 未结 12 2105
挽巷
挽巷 2021-02-15 03:20

When I advance the slider manually, the slider jumps to the top of the page. How can I prevent this from happening? Help much appreciated! Here\'s the code:

<         


        
相关标签:
12条回答
  • 2021-02-15 03:31

    you should try with jquery:

    $('.carousel-control').click(function (e) {
      e.preventDefault();
    });
    
    0 讨论(0)
  • 2021-02-15 03:32

    This one worked well for me. Replace href with "data-target" <a class="carousel-control right" data-slide="next" data-target="#carousel">

    0 讨论(0)
  • 2021-02-15 03:34

    You can either do this inline (less clean) :

    <a class="left carousel-control" role="button" data-slide="prev" 
      onclick="$(this).closest('.carousel').carousel('prev');">
      <span class="glyphicon glyphicon-chevron-left"></span>
    </a>
    <a class="right carousel-control" role="button" data-slide="next"  
      onclick="$(this).closest('.carousel').carousel('next');">
      <span class="glyphicon glyphicon-chevron-right"></span>
    </a>
    

    or you can do globally assuming you remove the href="#controlid :

    $('body').on('click','.carousel-control',function() {
       $(this).closest('.carousel').carousel( $(this).data('slide') );
    });
    

    or you can do it individually by being more specific in your selector

    $('body').on('click','#carouselID .carousel-control',function() {
       $(this).closest('.carousel').carousel( $(this).data('slide') );
    });
    
    0 讨论(0)
  • 2021-02-15 03:34

    Set href to href="javascript:void(0)" then add this somewhere in your js:

    $('.carousel-control.right').click(function(){ $('.carousel').carousel('next')});
    $('.carousel-control.left').click(function(){ $('.carousel').carousel('prev')});
    
    0 讨论(0)
  • 2021-02-15 03:37

    I've had the same problem

    my a tag looked like:

    <a class="carousel-control right" data-mixpanel-tracker="Slider Next" data-slide="next" href="#carousel">
    

    it was solved by removing extra mix-panel attributes in the tag:

    <a class="carousel-control right" data-slide="next" href="#carousel">
    

    sorry that it isn't solving your problem, but it might help others with similar problem.

    0 讨论(0)
  • 2021-02-15 03:38

    i tried to set up a working document of this. i don't see the jump behavior. did nothing out of the ordinary just copied your markup, added sample images, bootstrap.js, bootstrap-carosel.js, and bootstrap.css

    http://brandonam.com/bootTest/boot.html

    fyi: typical browser behavior for '#myCarousel' would be to point the window focus to the element marked '#myCarousel' ... but i think bootstrap prevents this by default. shouldn't be moving around.

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