我有一个图片库的下页,当您将鼠标悬停在图像上时,描述会滑出并放大图像。它工作正常。然而我想知道,而不是不得不用鼠标悬停我想访问页面和图库中的随机图像放大和描述滑出并恢复正常,然后移动到另一个图像,然后到下一个。所以,只需在访问该页面时澄清:
全部按随机顺序排列。
$(function() {
// View the selected photo at full size
$(".photo-image").click(function() {
$(this).addClass("photo-selected");
$(this).parent().addClass("photo-x");
$("#overlay").show();
});
// Close the full size view when #overlay is clicked
$("#overlay").click(function() {
$(".photo-image").removeClass("photo-selected");
$(".photo-x").removeClass("photo-x");
$("#overlay").hide();
});
});
// Close full size view if "esc"
$(document).keyup(function(e) {
if (e.keyCode == 27) {
$(".photo-image").removeClass("photo-selected");
$("#overlay").hide();
}
});

#container {
width: 850px;
position: relative;
margin: 0 auto;
top: 50px;
}
#overlay {
cursor: pointer;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: #000;
opacity: .7;
z-index: 2000;
display: none;
}
.photo {
position: relative;
float: left;
height: 100px;
width: 100px;
}
.photo-image {
cursor: pointer;
position: relative;
top: 1px;
left: 1px;
width: 100px;
border: 1px solid #999;
z-index: 1000;
opacity: 0.6;
transition: width 1s, top 1s, left 1s, opacity 1s, z-index .01s;
}
.photo-image:hover {
width: 200px;
top: -50px;
left: -50px;
z-index: 1001;
opacity: 1;
}
.photo-selected {
cursor: default;
z-index: 2001;
width: 500px;
opacity: 1;
top: -20px;
left: -200px;
box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
}
.photo-selected:hover {
width: 500px;
top: -50px;
left: -50px;
z-index: 2001;
opacity: 1;
top: -20px;
left: -200px;
}
.imagepluscontainer div.desc {
/* CSS for desc div of each image. */
position: absolute;
width: 90%;
z-index: 1;
/* Set z-index to that less than image's, so it's hidden beneath it */
bottom: 0;
/* Default position of desc div is bottom of container, setting it up to slide down */
left: 5px;
padding: 8px;
background: rgba(0, 0, 0, 0.8);
/* black bg with 80% opacity */
color: white;
-moz-border-radius: 0 0 8px 8px;
/* CSS3 rounded borders */
-webkit-border-radius: 0 0 8px 8px;
border-radius: 0 0 8px 8px;
opacity: 0;
/* Set initial opacity to 0 */
-moz-box-shadow: 0 0 6px rgba(0, 0, 0, 0.8);
/* CSS3 shadows */
-webkit-box-shadow: 0 0 6px rgba(0, 0, 0, 0.8);
box-shadow: 0 0 6px rgba(0, 0, 0, 0.8);
-moz-transition: all 0.5s ease 0.5s;
/* Enable CSS3 transition on desc div. Final 0.5s value is the delay before animation starts */
-webkit-transition: all 0.5s ease 0.5s;
-o-transition: all 0.5s ease 0.5s;
-ms-transition: all 0.5s ease 0.5s;
transition: all 0.5s ease 0.5s;
}
.imagepluscontainer div.desc a {
color: white;
}
.imagepluscontainer:hover div.desc {
/* CSS for desc div when mouse hovers over main container */
-moz-transform: translate(0, 100%);
-webkit-transform: translate(0, 100%);
-ms-transform: translate(0, 100%);
-o-transform: translate(0, 100%);
transform: translate(0, 100%);
opacity: 1;
/* Reveal desc DIV fully */
}
/*### Below CSS when applied to desc DIV slides the desc div from the right edge of the image ###*/
.imagepluscontainer div.rightslide {
width: 200px;
/* reset from default */
height: 100px;
top: -50px;
right: -50px;
left: auto;
/* reset from default */
bottom: auto;
/* reset from default */
padding-left: 15px;
-moz-border-radius: 0 8px 8px 0;
-webkit-border-radius: 0 8px 8px 0;
border-radius: 0 8px 8px 0;
}
.imagepluscontainer:hover div.rightslide {
-moz-transform: translate(100%, 0);
-webkit-transform: translate(100%, 0);
-ms-transform: translate(100%, 0);
-o-transform: translate(100%, 0);
transform: translate(100%, 0);
z-index: 2001;
}
/*### Below CSS when applied to desc DIV slides the desc div from the left edge of the image ###*/
.imagepluscontainer div.leftslide {
width: 280px;
/* reset from default */
height: 185px;
top: 0px;
left: -100;
bottom: auto;
/* reset from default */
padding-left: 15px;
-moz-border-radius: 8px 0 0 8px;
-webkit-border-radius: 8px 0 0 8px;
border-radius: 8px 0 0 8px;
z-index: 2001;
}
.imagepluscontainer:hover div.leftslide {
-moz-transform: translate(-100%, 0);
-webkit-transform: translate(-100%, 0);
-ms-transform: translate(-100%, 0);
-o-transform: translate(-100%, 0);
transform: translate(-100%, 0);
}
/*### Below CSS when applied to desc DIV slides the desc div from the top edge of the image ###*/
.imagepluscontainer div.upslide {
top: 0;
bottom: auto;
/* reset from default */
padding-bottom: 10px;
-moz-border-radius: 8px 8px 0 0;
-webkit-border-radius: 8px 8px 0 0;
border-radius: 8px 8px 0 0;
}
.imagepluscontainer:hover div.upslide {
-moz-transform: translate(0, -100%);
-webkit-transform: translate(0, -100%);
-ms-transform: translate(0, -100%);
-o-transform: translate(0, -100%);
transform: translate(0, -100%);
}

<div id="overlay"></div>
<div id="container">
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-1.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-2.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-3.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-4.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-5.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-6.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-1.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-2.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-3.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-4.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-5.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-6.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-1.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-2.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-3.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-4.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-5.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-6.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-1.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-2.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-3.jpg" alt="" class="photo-image" />
<div class="imagepluscontainer">
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-4.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-5.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
<div class="imagepluscontainer">
<div class="photo"><img src="http://s.cdpn.io/37045/wedding-6.jpg" alt="" class="photo-image" />
<div class="desc rightslide">Bdjfjffjkfjgjfgjfgjfgjfgjfjkg</div>
</div>
</div>
</div>
&#13;
以下是JSFiddle
答案 0 :(得分:0)
1.首先确保页面已满载,现在应该执行动画。
使用$( window ).load(function() { //all code written below here })
2.现在迭代每个孩子并暂停,然后再去下一个。使用.find()和How to add pause between each iteration of jQuery .each()? $('.imagepluscontainer').find('img').each(function () { //jquery animation code goes here
//remember to remove hover
// now pause between different itterations
};
如果您想随机化图像选择程序,我会发现遵循最简单的方法:
img
元素的数组。 eg- var imgplusArray = $(".imagepluscontainer").find("img").toArray();
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
shuffleArray(imgplusArray);
$(imgplusArray).each(function () { //jquery animation code goes here
//remember to remove hover
// now pause between different itterations
};
答案 1 :(得分:0)
您应该使用其他CSS
类,使用jQuery
为图像添加悬停效果。假设班级名称为hovered
。为CSS
和:hover
添加.imagepluscontainer
中使用的相同.photo-image
属性,如下所示。
.photo-image.hovered {
width: 200px;
top: -50px;
left: -50px;
z-index: 1001;
opacity: 1;
}
.imagepluscontainer.hovered div.desc {
-moz-transform: translate(0, 100%);
-webkit-transform: translate(0, 100%);
-ms-transform: translate(0, 100%);
-o-transform: translate(0, 100%);
transform: translate(0, 100%);
opacity: 1;
}
.imagepluscontainer.hovered div.rightslide {
-moz-transform: translate(100%, 0);
-webkit-transform: translate(100%, 0);
-ms-transform: translate(100%, 0);
-o-transform: translate(100%, 0);
transform: translate(100%, 0);
z-index: 2001;
}
.imagepluscontainer:hovered div.leftslide {
-moz-transform: translate(-100%, 0);
-webkit-transform: translate(-100%, 0);
-ms-transform: translate(-100%, 0);
-o-transform: translate(-100%, 0);
transform: translate(-100%, 0);
}
.imagepluscontainer.hovered div.upslide {
-moz-transform: translate(0, -100%);
-webkit-transform: translate(0, -100%);
-ms-transform: translate(0, -100%);
-o-transform: translate(0, -100%);
transform: translate(0, -100%);
}
您可以使用javascript
setInterval
功能在间隔后连续添加悬停效果。使用Math.random()
选择随机元素,如下所示。
setInterval(function(){
$('.imagepluscontainer').removeClass('hovered');
$('.photo-image').removeClass('hovered');
var x = Math.floor((Math.random() * ($('.imagepluscontainer').length-1)) + 0);
$('.imagepluscontainer').eq(x).addClass('hovered');
$('.photo-image').eq(x).addClass('hovered');
}, 2000);
希望这会对你有所帮助。