可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
<div class="row">
<div class="span12">
<script>
$('.carousel').carousel({
interval: 4000
})
</script>
<div class="container">
<div id="myCarousel" class="carousel slide frame">
<!-- Carousel items -->
<div class="carousel-inner">
<div class="active item"><a href="work.html"><img src="assets/images/slide_psd.jpg" width="1170" alt="wga"></a></div>
<div class="item"><a href="work.html"><img src="assets/images/slide_wga.jpg" width="1170" alt="wga"></a></div>
<div class="item"><a href="work.html"><img src="assets/images/slide_ts.jpg" width="1170" alt="top secret hair"></a></div>
<div class="item"><a href="work.html"><img src="assets/images/slide_baja.jpg" width="1170" alt="baja"></a></div>
</div>
<!-- Carousel nav -->
<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
</div>
</div>
<!-- end cara container-->
</div>
<!-- end span-->
</div>
<!-- end row-->
回答1:
You can add data-target attribute refering to your carousel: data-target="#carousel"
回答2:
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') );
});
回答3:
It turns out there was a smooth page jump script for another page that was causing this issue. It works after I moved out the the following from custom.js:
function filterPath(string) {
return string
.replace(/^\//,'')
.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
.replace(/\/$/,'');
}
var locationPath = filterPath(location.pathname);
var scrollElem = scrollableElement('html', 'body');
$('a[href*=#]').each(function() {
var thisPath = filterPath(this.pathname) || locationPath;
if ( locationPath == thisPath
&& (location.hostname == this.hostname || !this.hostname)
&& this.hash.replace(/#/,'') ) {
var $target = $(this.hash), target = this.hash;
if (target) {
var targetOffset = $target.offset().top;
$(this).click(function(event) {
event.preventDefault();
$(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
location.hash = target;
});
});
}
}
});
// use the first element that is "scrollable"
function scrollableElement(els) {
for (var i = 0, argLength = arguments.length; i <argLength; i++) {
var el = arguments[i],
$scrollElement = $(el);
if ($scrollElement.scrollTop()> 0) {
return el;
} else {
$scrollElement.scrollTop(1);
var isScrollable = $scrollElement.scrollTop()> 0;
$scrollElement.scrollTop(0);
if (isScrollable) {
return el;
}
}
}
return [];
}
回答4:
you should try with jquery:
$('.carousel-control').click(function (e) {
e.preventDefault();
});
回答5:
This one worked well for me. Replace href with "data-target"
<a class="carousel-control right" data-slide="next" data-target="#carousel">
回答6:
When you link to an HTML anchor, it will be relative to where the <div id="myCarousel">
is, which by default with Twitter Bootstrap, is located at the top of the carousel. I see that your using the data-
tags, therefore I don't believe there is a need for the href
attributes.
Change this:
<!-- Carousel nav -->
<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
</div>
to this:
<!-- Carousel nav -->
<a class="carousel-control left" data-slide="prev">‹</a>
<a class="carousel-control right" data-slide="next">›</a>
</div>
Currently I'm not at the office, so I haven't had time to test it in more than 1 scenario, however, from the looks, it should still work & give you your desired results.
回答7:
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.
回答8:
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')});
回答9:
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.
回答10:
We spent quite a lot of time on this but setting the data-target
attribute and other solutions did not work for us. The scroll-up/jumping of the page was happening irrespective.
This seems to happen with Bootstrap3 carousels where each slide is of different height. @Fabian's comment above helped us. For those who missed it, adding overflow:hidden to the carousel class is a good hack to fix this:
@media (min-width: 768px)
{
#our-carousel
{
overflow: hidden; /* workaround to resolve the page jumping/scrolling up on
smaller screens on auto/manual advancing of
carousel */
}
}
回答11:
I ended up adding a overflow: hidden; to the carousel class which did the trick.
回答12:
href="#myCarousel"
is looking for <a name='myCarousel'></a>
and trying to move the window to that position.
try setting your href to href="javascript:void(0)"