You need to enable JavaScript to run this app. 如何为已开发的横向滚动图片画廊实现指定hover悬停效果
优惠活动
大模型
产品
解决方案
定价
更多

如何为已开发的横向滚动图片画廊实现指定hover悬停效果

横向滚动图片画廊悬停效果实现需求

我已经完成了横向滚动图片画廊的基础开发,现在需要实现适配的图片hover悬停效果,目标效果为悬停时突出当前选中图片、其余图片弱化显示,以下是现有代码:

现有CSS代码

.cf:before, .cf:after {
    content: " ";
    display: table;
}
.cf:after {
    clear: both;
}
*, *:before, *:after {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
/*block level elements*/

.page {
    margin: 0px;
    padding: 0px;
}
.themains {
    margin: 0px;
    padding: 0px;
    max-width: none;
}
.inner {
    max-width: 1000px;
    margin: auto;
    padding-left: 15px;
    padding-right: 15px;
}
.scroll-container {
    position: relative;
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    width: 100%;
    height: 400px;
    scroll-snap-type: mandatory;
    scroll-behavior: smooth;
    background: #2C3E50;
    border: solid 7px yellow;
    font-size: 0px;
    margin-bottom: 50px;
}
.scroll-container .scroll-section {
    display: inline-block;
    vertical-align: middle;
    background: transparent;
    border: solid 5px #BFC9CA;
    white-space: nowrap;
}
/*Slider X (different sized images)*/

.scroll-container.diff-size {
    scroll-snap-destination: 50% 0%;
    padding: 20px 0px;
}
.scroll-container.diff-size .scroll-section {
    width: auto;
    height: 100%;
    margin-right: 15px;
    scroll-snap-coordinate: 50% 0%;
}
.scroll-container.diff-size .scroll-section:first-child {
    scroll-snap-coordinate: 0% 0%, 50% 0%;
    margin-left: 15px;
}
.scroll-container.diff-size .scroll-section:last-child {
    scroll-snap-coordinate: 50% 0%, 100% 0%;
}

现有HTML代码

<!DOCTYPE html>
<html lang='en'>
<head>
    <title>Gallery</title>
    <meta charset="UTF-8">
    <title>Horizontal Scroll Gallery</title>
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css" rel="stylesheet">
    <link href="style.css" rel="stylesheet">
</head>
<body>
    <article class="page">
        <article class="themains">
            <div class="scroll-intro" id="intro"></div>
            <div class="inner">
                <div class="scroll-container diff-size"><img class="scroll-section" src="https://cdn.pixabay.com/photo/2017/06/23/17/41/morocco-2435391__340.jpg"> <img class="scroll-section" src="https://cdn.pixabay.com/photo/2016/03/09/09/42/buildings-1245953__340.jpg"> <img class="scroll-section" src="https://cdn.pixabay.com/photo/2013/03/02/02/41/city-89197__340.jpg"> <img class="scroll-section" src="https://cdn.pixabay.com/photo/2020/03/27/08/57/waterfall-4972945_1280.jpg"> <img class="scroll-section" src="https://cdn.pixabay.com/photo/2012/11/28/10/37/rocket-launch-67649_1280.jpg"> <img class="scroll-section" src="https://cdn.pixabay.com/photo/2015/08/07/15/47/fireworks-879461_1280.jpg"> <img class="scroll-section" src="https://cdn.pixabay.com/photo/2012/03/03/23/06/backdrop-21534_1280.jpg"> <img class="scroll-section" src="https://cdn.pixabay.com/photo/2020/02/27/14/33/building-4884852__340.jpg"> <img class="scroll-section" src="https://cdn.pixabay.com/photo/2020/05/13/11/38/cherry-blossoms-5167156_1280.jpg"></div>
            </div>
            <p><br>
            <br></p>
        </article>
    </article><!-- partial -->
    <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'>
    </script>
</body>
</html>

实现方案

直接在现有CSS文件末尾新增以下样式即可,不需要修改原有HTML和JS逻辑:

/* 悬停效果新增样式 */
.scroll-container.diff-size .scroll-section {
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.scroll-container.diff-size:hover .scroll-section {
    filter: brightness(0.4) grayscale(0.3);
    transform: scale(0.92);
}
.scroll-container.diff-size .scroll-section:hover {
    filter: brightness(1) grayscale(0);
    transform: scale(1.08);
    z-index: 99;
    position: relative;
    cursor: pointer;
    box-shadow: 0 0 25px rgba(0,0,0,0.6);
}

以上样式实现的效果为:鼠标移入画廊区域时,所有图片默认变暗变灰、小幅缩小;鼠标悬停到指定图片时,当前图片恢复原色、放大突出,同时添加阴影增强层级感,所有变化都带平滑过渡动画,完全适配你现有的横向滚动逻辑。

内容的提问来源于stack exchange,提问作者Edmond Helen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.17 20:45:02