简体   繁体   中英

How to scroll to Next thumbnail image on photo gallery

I have found a code that I am using to create a photo gallery on my web page. It is a standard click on thumbnail to see large photo function. It is working fine for me. The only thing is that I am having trouble in adding scroll to next thumbnail image function to it. The current code only shows 5 thumbnails. I can add more but they are being displayed in multiple rows. I want to have one row of thumbnails and simply scroll to next. Has anybody dealt with this before? Here is my web page code:

`<html>
<head>
<title>Photo Gallery</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<div id="gallery">

    <div id="bigimages">
        <div id="normal1">
            <img src="bigimage1.png" alt=""/>
        </div>

        <div id="normal2">
            <img src="bigimage2.png" alt=""/>
        </div>

        <div id="normal3">
            <img src="bigimage3.png" alt=""/>
        </div>

        <div id="normal4">
            <img src="bigimage4.png" alt=""/>
        </div>

        <div id="normal5">
            <img src="bigimage5.png" alt=""/>
        </div>

    </div>

    <div id="thumbs">
        <a href="javascript: changeImage(1);"><img src="image1.png" alt="" border="0" /></a>
        <a href="javascript: changeImage(2);"><img src="image2.png" alt="" /></a>
        <a href="javascript: changeImage(3);"><img src="image3.png" alt="" /></a>
        <a href="javascript: changeImage(4);"><img src="image4.png" alt="" /></a>
        <a href="javascript: changeImage(5);"><img src="image5.png" alt="" /></a>
    </div>
</div>

`

Here is my CSS code:

`<style type="text/css">
<!--
body {
margin: 0;
padding: 0;
background: #222;
color: #EEE;
text-align: center;
font: normal 9pt Verdana;
}
a:link, a:visited {
color: #EEE;
}
img {
border: none;
}
#normal2, #normal3, #normal4, #normal5 {
display: none;
}
#gallery {
margin: 0 auto;
width: 800px;
}
#thumbs {
margin: 10px auto 10px auto;
text-align: center;
width: 800px;
}
#bigimages {
width: 770px;
float: left;
}
#thumbs img {
width: 130px;
height: 130px;
}
#bigimages img {
border: 4px solid #555;
margin-top: 5px;
width: 750px;
}
#thumbs a:link, #thumbs a:visited {
width: 130px;
height: 130px;
border: 6px solid #555;
margin: 6px;
float: left;
}
#thumbs a:hover {
border: 6px solid #888;
}
-->
</style>`

And finally, here is my JavaScript code:

`<script type="text/javascript">
function changeImage(current) {
var imagesNumber = 5;

for (i=1; i<=imagesNumber; i++) {
    if (i == current) {
        document.getElementById("normal" + current).style.display = "block";
    } else {
        document.getElementById("normal" + i).style.display = "none";
    }
}
}

</script>`

Thank you.

You can create another container inside of the #thumbs div that is wide enough to fit all of the thumbnails needed in one row, and then set the #thumbs divs overflow to scroll, so the thumbnails that are beyond the width of the gallery are hidden until scrolled to.

 function changeImage(current) { var imagesNumber = 7; for (i=1; i<=imagesNumber; i++) { if (i == current) { document.getElementById("normal" + current).style.display = "block"; } else { document.getElementById("normal" + i).style.display = "none"; } } } 
 body { margin: 0; padding: 0; background: #222; color: #EEE; text-align: center; font: normal 9pt Verdana; } a:link, a:visited { color: #EEE; } img { border: none; } #normal2, #normal3, #normal4, #normal5, #normal6, #normal7 { display: none; } #gallery { margin: 0 auto; width: 800px; } #thumbs { text-align: center; width: 770px; overflow: scroll; } #thumbs #container { width: 1500px } #bigimages { width: 770px; float: left; } #thumbs img { width: 130px; height: 130px; } #bigimages img { border: 4px solid #555; margin-top: 5px; width: 770px; } #thumbs a:link, #thumbs a:visited { width: 130px; height: 130px; border: 6px solid #555; margin: 6px; float: left; } #thumbs a:hover { border: 6px solid #888; } 
 <html> <head> <title>Photo Gallery</title> <link rel="stylesheet" type="text/css" href="style.css"/> </head> <body> <div id="gallery"> <div id="bigimages"> <div id="normal1"> <img src="http://placehold.it/750x100?text=1" alt=""/> </div> <div id="normal2"> <img src="http://placehold.it/750x100?text=2" alt=""/> </div> <div id="normal3"> <img src="http://placehold.it/750x100?text=3" alt=""/> </div> <div id="normal4"> <img src="http://placehold.it/750x100?text=4" alt=""/> </div> <div id="normal5"> <img src="http://placehold.it/750x100?text=5" alt=""/> </div> <div id="normal6"> <img src="http://placehold.it/750x100?text=6" alt=""/> </div> <div id="normal7"> <img src="http://placehold.it/750x100?text=7" alt=""/> </div> </div> <div id="thumbs"> <div id="container"> <a href="javascript: changeImage(1);"><img src="http://placehold.it/200x200?text=1" alt="" border="0" /></a> <a href="javascript: changeImage(2);"><img src="http://placehold.it/200x200?text=2" alt="" /></a> <a href="javascript: changeImage(3);"><img src="http://placehold.it/200x200?text=3" alt="" /></a> <a href="javascript: changeImage(4);"><img src="http://placehold.it/200x200?text=4" alt="" /></a> <a href="javascript: changeImage(5);"><img src="http://placehold.it/200x200?text=5" alt="" /></a> <a href="javascript: changeImage(6);"><img src="http://placehold.it/200x200?text=6" alt="" /></a> <a href="javascript: changeImage(7);"><img src="http://placehold.it/200x200?text=7" alt="" /></a> </div> </div> </div> 

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