【问题标题】:How to scroll to Next thumbnail image on photo gallery如何滚动到照片库上的下一个缩略图
【发布时间】:2015-09-01 00:08:26
【问题描述】:

我找到了用于在我的网页上创建照片库的代码。 这是一个标准的点击缩略图查看大照片功能。它对我来说很好。唯一的问题是我无法将滚动添加到下一个缩略图图像功能。当前代码仅显示 5 个缩略图。我可以添加更多,但它们显示在多行中。我想要一排缩略图,然后简单地滚动到下一个。以前有人处理过这个吗? 这是我的网页代码:

`<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>

`

这是我的 CSS 代码:

`<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>`

最后,这是我的 JavaScript 代码:

`<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>`

谢谢。

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    您可以在 #thumbs div 内创建另一个容器,该容器的宽度足以容纳一行所需的所有缩略图,然后将 #thumbs div 设置为溢出以滚动,这样超出画廊在滚动到之前是隐藏的。

    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>

    【讨论】:

    • 您好,我尝试通过隐藏容器并添加左右链接/按钮来修改代码,但无法使其正常工作。
    猜你喜欢
    • 1970-01-01
    • 2012-05-24
    • 2012-11-12
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    相关资源
    最近更新 更多