我使用HTML和PHP创建了一个图像轮播。
图像轮播的工作原理如下:
但是,有时在鼠标悬停缩略图图像后,图像轮播似乎会同时发生2个“活动”类。因此,它在原始图像的正下方创建了第二个图像轮播。但是一旦原始图像轮播自动滑动到队列中的下一个图像,第二个图像轮播将自动消失。
我该如何解决这个问题?
提前致谢。
<div id='carouselCustom' class='carousel slide' data-ride='carousel'>
<div class='carousel-outer'>
<!-- Wrapper for slides -->
<div class='carousel-inner'>
<?php $bannerCnt=1; $bannerCnt2=0; foreach($images as $image) { ?>
<div id="<?php echo 'slide_'.$bannerCnt2; ?>" class="<?php if($bannerCnt == 1) { echo 'active'; } ?> item">
<a id="imagezoom" href="<?php echo $image['url']; ?>">
<img id="zoomImage" class="img-responsive" src="<?php echo BANNER_PATH.'assets/landing_banner_images/'.$image['banner_path']; ?>" height="585px" data-bgfit="cover" data-bgposition="center center" data-bgrepeat="no-repeat" onmouseover="bannerPreview()" onmouseout="bannerOffPreview()"/>
</a>
</div>
<?php $bannerCnt++; $bannerCnt2++; } ?>
<div id='banner-mouseover-area' style="position: absolute;bottom: 0px; display: none;">
<!-- Indicators -->
<ol class='carousel-indicators mCustomScrollbar'>
<?php $thumbnailCnt=0; foreach($images as $imageThumb) { ?>
<li data-transition="flip" data-target="#carouselCustom" data-change-to="<?php echo $thumbnailCnt; ?>" class="<?php if($thumbnailCnt == 0) { echo 'active'; } ?>">
<a onmouseover='return zoom_image($(this));' rel="<?php echo BANNER_PATH.'assets/landing_banner_images/'.$imageThumb['banner_path'].'_&'.$thumbnailCnt; ?>" href="<?php echo $imageThumb['url']; ?>">
<img class="img-responsive" src="<?php echo BANNER_PATH.'assets/landing_banner_images/'.$imageThumb['banner_path']; ?>" width="160px" onmouseover="bannerPreview()" />
</a>
</li>
<?php $thumbnailCnt++; } ?>
</ol>
</div>
</div>
<!-- Controls -->
<!-- Left -->
<div id="leftControl" style="position: absolute; margin-top: -20px; left: 20px;" class="tp-leftarrow tparrows default round" data-target="#carouselCustom" data-slide='prev'>
<div class="tp-arr-allwrapper">
<div class="tp-arr-iwrapper">
<div class="tp-arr-imgholder" style="visibility: inherit; opacity: 1; background-image: url("undefined");"></div>
<div class="tp-arr-imgholder2"></div>
<div class="tp-arr-titleholder"></div>
<div class="tp-arr-subtitleholder"></div>
</div>
</div>
</div>
<!-- Right -->
<div id="rightControl" style="position: absolute; margin-top: -20px; right: 20px;" class="tp-rightarrow tparrows default round" data-target="#carouselCustom" data-slide='next'>
<div class="tp-arr-allwrapper">
<div class="tp-arr-iwrapper">
<div class="tp-arr-imgholder" style="visibility: inherit; opacity: 1; background-image: url("undefined");"></div>
<div class="tp-arr-imgholder2"></div>
<div class="tp-arr-titleholder"></div>
<div class="tp-arr-subtitleholder"></div>
</div>
</div>
</div>
</div>
</div>
function zoom_image(img) {
//Get the rel value of the image
var div_id = '';
var fullpath = img.attr('rel');
var splitpath = fullpath.split("_&");
var image_name = splitpath[0];
var slide_id = splitpath[1];
var new_slide_id = "slide_" + slide_id;
document.getElementById('zoomImage').src = image_name;
var currentSlide = document.getElementsByClassName('active item')[0].id;
console.log(new_slide_id);
//For loop to remove class and add class to the selected image
for (i = 0; i < imagesCount; i++) {
div_id = "slide_" + i;
if (div_id !== new_slide_id) {
//Remove all active class from the div
var removeB = document.getElementById(div_id);
removeB.className = "";
removeB.className = "item";
} else {
//Add active class to the div
var mouseoverSlide = document.getElementById(new_slide_id);
mouseoverSlide.className = "";
mouseoverSlide.className = "item active";
}
}
}
答案 0 :(得分:0)
好吧,既然您正在使用Twitter Bootstrap,那么您很可能也在使用jQuery(至少Bootstrap使用它),那么为什么不利用它呢?
您可以执行以下操作:
function zoom_image(img) {
//Get the rel value of the image
var div_id = '';
var fullpath = img.attr("rel");
var splitpath = fullpath.split("_&");
var image_name = splitpath[0];
var slide_id = splitpath[1];
var new_slide_id = "slide_" + slide_id;
$('#zoomImage').attr("src", image_name);
$(".active.item").removeClass("active");
$("#"+new_slide_id).addClass("active");
}
我还没有测试过这段代码,试一试
================编辑================
重新阅读代码后,您可以将其简化一点:
对于初学者,您可以在代码中使用data- *属性,这样您就不必为了达到图像的ID而奋斗。你在哪里:
<a onmouseover='return zoom_image($(this));' ...>
你可以拥有像
这样的东西<a onmouseover='return zoom_image($(this));' data-image="<?php echo BANNER_PATH.'assets/landing_banner_images/'.$imageThumb['banner_path']; ?>" data-target="<?php echo 'slide_".$thumbnailCnt; ?>" ...>
然后简单地说:
function zoom_image(img) {
var image_name = img.attr("data-image");
var new_slide_id = img.attr("data-target");
$('#zoomImage').attr("src", image_name);
$(".active.item").removeClass("active");
$("#"+new_slide_id).addClass("active");
}