You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现点击缩略图后放大图片并显示在页面底部

图片画廊点击缩略图在底部放大显示的实现方案

需求说明

已制作主题为Image gallery的网页,插入了若干缩略图,需要实现:用户点击缩略图时,图片放大并显示在页面底部的指定区域。

修改后的完整代码

HTML代码

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" type="text/css" href="image_gallery.css">
        <title>Image Gallery</title>
    </head>

    <body class="FullBody">
        <div class="Heading">
            Responsive Image Gallery
        </div>
        <div class="AllImages">
            <img src="1.jpg" alt="1st image">
            <img src="2.jpg" alt="2nd image">
            <img src="3.jpg" alt="3rd image">
            <img src="4.jpeg" alt="4th image">
            <img src="5.jpg" alt="5th image">
            <img src="6.jpg" alt="6th image">
            <img src="7.jpg" alt="7th image">
            <img src="8.jpg" alt="8th image">
            <img src="9.jpg" alt="9th image">
            <img src="10.jpg" alt="10th image">
        </div>
        <!-- 底部大图显示容器 -->
        <div class="LargeImageContainer">
            <img id="largeImage" src="" alt="点击缩略图查看大图">
        </div>

        <footer class="Footer">
            <span class="FooterRight">Copyright - Harsh Jain</span>
            <span class="FooterLeft">Created - 03/07/2024</span>
        </footer>
        <!-- 点击切换逻辑脚本 -->
        <script>
            const thumbnails = document.querySelectorAll('.AllImages img');
            const largeImage = document.getElementById('largeImage');

            thumbnails.forEach(thumbnail => {
                thumbnail.addEventListener('click', function() {
                    largeImage.src = this.src;
                    largeImage.alt = this.alt;
                });
            });
        </script>
    </body>
</html>

CSS代码

.FullBody
{
    background-image: url('BackgroundImage.jpg');
    background-size: cover;
    margin: 0;
    padding-bottom: 140px; /* 预留底部元素空间,避免内容被遮挡 */
}
.Heading
{
    font-family: Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
    font-size: xx-large;
    text-align: center;
    color: azure;
    height: 100px;
    line-height: 100px; /* 实现文字垂直居中 */
}
.Footer
{
    height:40px;
    background-color: black;
    color: white;
    font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
    font-size: medium;
    width:100%;
    position: fixed;
    bottom: 0px;
    left:0px;
    line-height: 40px;
    padding: 0 10px;
    box-sizing: border-box;
}
.FooterLeft
{
    float: left;
}
.FooterRight
{
    float: right;
}
.AllImages img
{
    height:100px;
    width:9.78%;
    border:2px solid whitesmoke;
    border-radius: 2px;
    float:left;
    margin: 0 0.1%; /* 微调间距避免换行 */
    cursor: pointer; /* 鼠标悬停显示手型,提示可点击 */
}
.AllImages
{
    margin-left:10px;
    margin-right:10px;
    margin-top:2px;
    margin-bottom: 2px;
    overflow: hidden; /* 清除浮动 */
}
/* 底部大图容器样式 */
.LargeImageContainer
{
    position: fixed;
    bottom: 40px; /* 固定在footer上方 */
    left: 0;
    width: 100%;
    height: 100px;
    background-color: rgba(0,0,0,0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px;
    box-sizing: border-box;
}
#largeImage
{
    max-height: 100%;
    max-width: 100%;
    border: 2px solid white;
}

关键改动说明

  • HTML:新增底部大图容器和点击切换的JavaScript脚本,专门用于承载放大后的图片。
  • CSS:
    • 给页面主体添加底部内边距,避免内容被底部固定元素遮挡;
    • 优化文字垂直居中方式,提升视觉效果;
    • 给缩略图添加手型光标,明确交互提示;
    • 定义底部大图容器的固定定位样式,深色背景突出显示放大后的图片。
  • JavaScript:遍历所有缩略图,给每个缩略图绑定点击事件,点击时将大图的地址和描述替换为当前缩略图的对应属性值,实现大图切换显示。

内容的提问来源于stack exchange,提问作者Harsh jain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 02:57:33