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

SCSS变量与JavaScript setProperty失效,图片切换滑块功能异常求助

问题描述

我需要实现拖动滑块左右移动时显示两张图片的对比效果,但运行代码时出现报错,setProperty方法未按预期工作,尝试纯CSS实现也存在问题,怀疑是JavaScript代码异常导致,求解答。

相关代码

HTML

<main>
    <div class="container">
        <div class="image-container">
            <img class="image-before slider-image"
                src="/img/before.png" alt="before image">
            <img class="image-after slider-image"
            src="/img/after.png" alt="after image">
        </div>
        <input type="range" min="0" step="10" max="100" value="50" class="slider" aria-label="Percentage of before photo shown" />
        <div class="slider-line">
            <div class="slider-button" aria-hidden="true">
                <svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" viewBox="0 0 256 256"><rect width="256" height="256" fill="none"></rect><line x1="128" y1="40" x2="128" y2="216" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></line><line x1="96" y1="128" x2="16" y2="128" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></line><polyline points="48 160 16 128 48 96" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></polyline><line x1="160" y1="128" x2="240" y2="128" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></line><polyline points="208 96 240 128 208 160" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"></polyline></svg>
            </div>
        </div>
    </div>
</main>

SCSS

*,
*::after,
*::before {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

$white : #dddddd;
$box-shadow: 1px 1px 1px hsl(0, 50%, 2%, .5);

img {
    display: block;
    max-width: 100%;
}

main {
    display: grid;
    place-items: center;
    min-height: 100vh;
}

.container {
    display: grid;
    place-content: center;
    position: relative;
    overflow: hidden;
    border-radius: 1rem;
    --position: 50%; /* 替换原SCSS变量为CSS自定义属性 */
}

.image-container {
    max-width: 800px;
    max-height: 90vh;
    aspect-ratio: 1/1;
}

.slider-image{
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: left;
}

.image-before {
    position: absolute;
    inset: 0;
    width: var(--position); /* 使用自定义属性 */
}

.slider {
    position: absolute;
    inset: 0;
    cursor: pointer;
    opacity: 0;
    width: 100%;
    height: 100%;

    &:focus-visible ~ .slider-button{
        outline: 5px solid black;
        outline-offset: 3px;
    }
}

.slider-line {
    position: absolute;
    inset: 0;
    width: 0.2rem;
    height: 100%;
    background-color: $white;
    z-index: 10;
    left: var(--position); /* 使用自定义属性 */
    transform: translateX(-50%);
    pointer-events: none;
}

.slider-button {
    position: absolute;
    background-color: $white;
    padding: .5rem;
    border-radius: 100vw;
    display: grid;
    place-items: center;
    top: 50%;
    left: var(--position); /* 使用自定义属性 */
    transform: translateX(-50%);
    pointer-events: none;
    z-index: 100;
    box-shadow: $box-shadow;
}

JavaScript

const container = document.querySelector(".container");

document.querySelector(".slider").addEventListener("input", (e) => {
  // 修正语法错误,使用CSS自定义属性名,参数传递正确
  container.style.setProperty('--position', `${e.target.value}%`);
});
问题解决说明
  1. 替换SCSS变量为CSS自定义属性:JavaScript无法直接操作SCSS编译前的变量,必须改用CSS原生自定义属性(--xxx格式),这样JS才能通过setProperty动态修改属性值。
  2. 修正JavaScript语法错误:原代码中setProperty的括号位置错误,且错误使用了SCSS变量名作为属性名,需改为自定义属性名--position,同时确保参数正确传递到方法内部。

内容的提问来源于stack exchange,提问作者A N

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 09:45:28