前后图像滑块不起作用。

huangapple go评论69阅读模式
英文:

before and after image slider not working

问题

对不起,提前说一声,如果有任何错误或糟糕的编码。

我已经根据以下原始教程进行了适应:https://www.youtube.com/watch?v=dzqDU9efnnk&t=61s

当我将其适应到我的代码中时,似乎存在问题,但我不确定问题出在哪里。我能看到的三个主要问题是左侧图像大于右侧图像,整个容器周围有大的填充,以及滑块的 JavaScript 不起作用。

如果您能帮助解决这个问题,那将不胜感激。

请记住,我在编码方面有点笨,所以如果您能尽量简化回答,那也将不胜感激。

HTML:

<main class="led-main">
    <div class="led-container">
        <div class="led-image-container">
            <img
                class="led-image-before led-slider-image"
                src="/images/products/multiservice-chilled-beams/led-lighting-replacement/101-new-cavendish-before-led-lighting-upgrade.jpg"
                alt="Before LED Lighting Upgrade" title="Image showing the project before the LED lighting upgrade"
                loading="lazy"
            />
            <img
                class="led-image-after led-slider-image"
                src="/images/products/multiservice-chilled-beams/led-lighting-replacement/101-new-cavendish-after-led-lighting-upgrade.jpg"
                alt="After LED Lighting Upgrade" title="Image showing the project after the LED lighting upgrade"
                loading="lazy"
            />
        </div>
        <input type="range" min="0" step="10" max="100" value="50" class="led-slider" aria-label="Percentage of before photo shown"/>
        <div class="slider-line"></div>
        <div class="led-slider-button" aria-hidden="true">
            <svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="#000000" viewBox="0 0 256 256">
                <path d="M136,40V216a8,8,0,0,1-16,0V40a8,8,0,0,1,16,0ZM96,120H35.31l18.35-18.34A8,8,0,0,0,42.34,90.34l-32,32a8,8,0,0,0,0,11.32l32,32a8,8,0,0,0,11.32-11.32L35.31,136H96a8,8,0,0,0,0-16Zm149.66,2.34-32-32a8,8,0,0,0-11.32,11.32L220.69,120H160a8,8,0,0,0,0,16h60.69l-18.35,18.34a8,8,0,0,0,11.32,11.32l32-32A8,8,0,0,0,245.66,122.34Z"></path>
            </svg>
        </div><br>
    </div>
</main>

CSS:

.led-main{
    display: grid;
    place-items: center;
    min-height: 100vh;
}
.led-container{
    display: grid;
    place-content: center;
    position: relative;
    overflow: hidden;
    border-radius: 1rem;
    --position: 50%;
}
.led-image-container{
    max-width: 800px;
    max-height: 90vh;
    aspect-ratio: 16/9;
}
.led-slider-image{
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: left;
}

.led-image-before{
    position: absolute;
    inset: 0;
    width: var(--position);
}
.led-slider{
    position: absolute;
    inset: 0;
    cursor: pointer;
    opacity: 0;
    width: 100%;
    height: 100%;
}
.led-slider:focus-visible ~ .led-slider-button {
    outline: 2px solid black;
    outline-offset: 3px;
}
.slider-line{
    position: absolute;
    inset: 0;
    width: .2rem;
    height: 100%;
    background-color: #fff;
    z-index: 10;
    left: var(--position);
    transform: translateX(-50%);
    pointer-events: none;
}

.led-slider-button{
    position: absolute;
    background-color: #fff;
    color: black;
    padding: .5rem;
    border-radius: 100vw;
    display: grid;
    place-items: center;
    top: 50%;
    left: var(--position);
    transform: translate(-50%, -50%);
    pointer-events: none;
    box-shadow: 1px 1px 1px hsl(0, 50%, 2%, .5);
}

JavaScript:

<script type="text/javascript">
    const container = document.querySelector('led-container');
    document.querySelector('.led-slider').addEventListener('input', (e) => {
        container.style.setProperty('--position', `${e.target.value}%`);
    })
</script>
英文:

sorry in advance for any wrong/poor coding.

I've adapted the following from this original tutorial: https://www.youtube.com/watch?v=dzqDU9efnnk&amp;t=61s

When I've adapted it to my code there seems to be an issue and I'm unsure where in the code. Three major problems I can see with it is the left image being bigger than the right, large padding around the whole container, and the slider javascript not working.

Any help on this would be greatly appreciated.

Please bear in mind I'm a bit of an idiot when it comes to coding skills so if you could dumb down your answer as much as possible that would be also greatly appreciated.

HTML:

&lt;main class=&quot;led-main&quot;&gt;
&lt;div class=&quot;led-container&quot;&gt;
&lt;div class=&quot;led-image-container&quot;&gt;
&lt;img
class=&quot;led-image-before led-slider-image&quot;
src=&quot;/images/products/multiservice-chilled-beams/led-lighting-replacement/101-new-cavendish-before-led-lighting-upgrade.jpg&quot;
alt=&quot;Before LED Lighting Upgrade&quot; title=&quot;Image showing the project before the LED lighting upgrade&quot;
loading=&quot;lazy&quot;
/&gt;
&lt;img
class=&quot;led-image-after led-slider-image&quot;
src=&quot;/images/products/multiservice-chilled-beams/led-lighting-replacement/101-new-cavendish-after-led-lighting-upgrade.jpg&quot;
alt=&quot;After LED Lighting Upgrade&quot; title=&quot;Image showing the project after the LED lighting upgrade&quot;
loading=&quot;lazy&quot;
/&gt;
&lt;/div&gt;
&lt;input type=&quot;range&quot; min=&quot;0&quot; step=&quot;10&quot; max=&quot;100&quot; value=&quot;50&quot; class=&quot;led-slider&quot; aria-label=&quot;Percentage of before photo shown&quot;/&gt;
&lt;div class=&quot;slider-line&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;led-slider-button&quot; aria-hidden=&quot;true&quot;&gt;
&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;30&quot; height=&quot;30&quot; fill=&quot;#000000&quot; viewBox=&quot;0 0 256 256&quot;&gt;&lt;path d=&quot;M136,40V216a8,8,0,0,1-16,0V40a8,8,0,0,1,16,0ZM96,120H35.31l18.35-18.34A8,8,0,0,0,42.34,90.34l-32,32a8,8,0,0,0,0,11.32l32,32a8,8,0,0,0,11.32-11.32L35.31,136H96a8,8,0,0,0,0-16Zm149.66,2.34-32-32a8,8,0,0,0-11.32,11.32L220.69,120H160a8,8,0,0,0,0,16h60.69l-18.35,18.34a8,8,0,0,0,11.32,11.32l32-32A8,8,0,0,0,245.66,122.34Z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;
&lt;/div&gt;&lt;br&gt;

</main>

CSS:

.led-main{
display: grid;
place-items: center;
min-height: 100vh;
}
.led-container{
display: grid;
place-content: center;
position: relative;
overflow: hidden;
border-radius: 1rem;
--position: 50%
}
.led-image-container{
max-width: 800px;
max-height: 90vh;
aspect-ratio: 16/9;
}
.led-slider-image{
width: 100%;
height: 100%;
object-fit: cover;
object-position: left;
}
.led-image-before{
position: absolute;
inset: 0;
width: var(--position);
}
.led-slider{
position: absolute;
inset: 0;
cursor: pointer;
opacity: 0;
width: 100%;
height: 100%;
}
.led-slider:focus-visible ~ .led-slider-button {
outline: 2px solid black;
outline-offset: 3px;
}
.slider-line{
position: absolute;
inset: 0;
width: .2rem;
height: 100%;
background-color: #fff;
z-index: 10;
left: var(--position);
transform: translateX(-50%)
pointer-events: none;
}
.led-slider-button{
position: absolute;
background-color: #fff;
color: black;
padding: .5rem;
border-radius: 100vw;
display: grid;
place-items: center;
top: 50%;
left: var(--position);
transform: translate(-50%, -50%);
pointer-events: none;
box-shadow: 1px 1px 1px hsl(0, 50%, 2%, .5);
}

JavaScript:

&lt;script type=&quot;text/javascript&quot;&gt;
const container = document.querySelector(&#39;led-container&#39;);
document.querySelector(&#39;.led-slider&#39;).addEventListener
(&#39;input&#39; (e) =&gt; {
container.style.setProperty(&#39;--position&#39;, 
`${e.target.value}%`)
})
&lt;/script&gt;

答案1

得分: 1

您的图片缺少"display-block"属性。

只需将以下代码添加到您的代码中,两张图片就应该具有相同的大小。

img {
  display: block;
}

如果您需要关于"display"或"display block"的更多信息,您可以在Mozillaw3schools找到相关信息。

以下是应用"display block"属性后您提供的代码示例(带有一些占位图片):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
const container = document.querySelector('led-container');
document.querySelector('.led-slider').addEventListener('input', (e) => {
  container.style.setProperty('position', `${e.target.value}%`)
})

<!-- language: lang-css -->
img {
  display: block;
}

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

/* 其余 CSS 样式省略 */

<!-- language: lang-html -->
<main class="led-main">
  <div class="led-container">
    <div class="led-image-container">
      <img class="led-image-before led-slider-image" src="https://techcrunch.com/wp-content/uploads/2021/07/GettyImages-1207206237.jpg?w=1390&crop=1" alt="Before LED Lighting Upgrade" title="Image showing the project before the LED lighting upgrade" loading="lazy" />
      <img class="led-image-after led-slider-image" src="https://static01.nyt.com/images/2020/12/14/well/14google-photo/merlin_178797099_f0a14fdb-8c54-4aad-ba09-4506303b06c3-superJumbo.jpg?quality=75&auto=webp" alt="After LED Lighting Upgrade" title="Image showing the project after the LED lighting upgrade" loading="lazy" />
    </div>
    <!-- 其余 HTML 省略 -->
  </div>
</main>
英文:

You are missing a display-block on your images

Simply add this to your code and both images should be the same size.

img {
display: block
}

If you need some more information about display / display block you can find that here Mozilla or here w3schools

<br><br>

The code you provided in action with display block applied to the images: With some placeholder images

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const container = document.querySelector(&#39;led-container&#39;);
document.querySelector(&#39;.led-slider&#39;).addEventListener(&#39;input&#39;, (e) =&gt; {
container.style.setProperty(&#39;position&#39;,
`${e.target.value}%`)
})

<!-- language: lang-css -->

img {
display: block
}
.led-main {
display: grid;
place-items: center;
min-height: 100vh;
}
.led-container {
display: grid;
place-content: center;
position: relative;
overflow: hidden;
border-radius: 1rem;
--position: 50%
}
.led-image-container {
max-width: 800px;
max-height: 90vh;
aspect-ratio: 16/9;
}
.led-slider-image {
width: 100%;
height: 100%;
object-fit: cover;
object-position: left;
}
.led-image-before {
position: absolute;
inset: 0;
width: var(--position);
}
.led-slider {
position: absolute;
inset: 0;
cursor: pointer;
opacity: 0;
width: 100%;
height: 100%;
}
.led-slider:focus-visible~.led-slider-button {
outline: 2px solid black;
outline-offset: 3px;
}
.slider-line {
position: absolute;
inset: 0;
width: .2rem;
height: 100%;
background-color: #fff;
z-index: 10;
left: var(--position);
transform: translateX(-50%) pointer-events: none;
}
.led-slider-button {
position: absolute;
background-color: #fff;
color: black;
padding: .5rem;
border-radius: 100vw;
display: grid;
place-items: center;
top: 50%;
left: var(--position);
transform: translate(-50%, -50%);
pointer-events: none;
box-shadow: 1px 1px 1px hsl(0, 50%, 2%, .5);
}

<!-- language: lang-html -->

&lt;main class=&quot;led-main&quot;&gt;
&lt;div class=&quot;led-container&quot;&gt;
&lt;div class=&quot;led-image-container&quot;&gt;
&lt;img class=&quot;led-image-before led-slider-image&quot; src=&quot;https://techcrunch.com/wp-content/uploads/2021/07/GettyImages-1207206237.jpg?w=1390&amp;crop=1&quot; alt=&quot;Before LED Lighting Upgrade&quot; title=&quot;Image showing the project before the LED lighting upgrade&quot; loading=&quot;lazy&quot;
/&gt;
&lt;img class=&quot;led-image-after led-slider-image&quot; src=&quot;https://static01.nyt.com/images/2020/12/14/well/14google-photo/merlin_178797099_f0a14fdb-8c54-4aad-ba09-4506303b06c3-superJumbo.jpg?quality=75&amp;auto=webp&quot; alt=&quot;After LED Lighting Upgrade&quot; title=&quot;Image showing the project after the LED lighting upgrade&quot;
loading=&quot;lazy&quot; /&gt;
&lt;/div&gt;
&lt;input type=&quot;range&quot; min=&quot;0&quot; step=&quot;10&quot; max=&quot;100&quot; value=&quot;50&quot; class=&quot;led-slider&quot; aria-label=&quot;Percentage of before photo shown&quot; /&gt;
&lt;div class=&quot;slider-line&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;led-slider-button&quot; aria-hidden=&quot;true&quot;&gt;
&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;30&quot; height=&quot;30&quot; fill=&quot;#000000&quot; viewBox=&quot;0 0 256 256&quot;&gt;&lt;path d=&quot;M136,40V216a8,8,0,0,1-16,0V40a8,8,0,0,1,16,0ZM96,120H35.31l18.35-18.34A8,8,0,0,0,42.34,90.34l-32,32a8,8,0,0,0,0,11.32l32,32a8,8,0,0,0,11.32-11.32L35.31,136H96a8,8,0,0,0,0-16Zm149.66,2.34-32-32a8,8,0,0,0-11.32,11.32L220.69,120H160a8,8,0,0,0,0,16h60.69l-18.35,18.34a8,8,0,0,0,11.32,11.32l32-32A8,8,0,0,0,245.66,122.34Z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;
&lt;/div&gt;&lt;br&gt;

<!-- end snippet -->

答案2

得分: 0

代码部分不要翻译,只返回翻译好的部分:

第一个问题出现在您的JavaScript代码中。事件监听器函数中存在语法错误。在'input'事件之后,您漏掉了一个逗号,漏掉了一个闭括号,而且您应该使用.led-container而不是led-container。以下是已更正的代码:

const container = document.querySelector('.led-container');
document.querySelector('.led-slider').addEventListener('input', (e) => {
    container.style.setProperty('--position', `${e.target.value}%`);
});
英文:

The first issue is with your JavaScript code. There is a syntax error in the event listener function. You are missing a comma after the 'input' event and a closing parenthesis and you should use .led-container instead of led-container. Here's the corrected code:

const container = document.querySelector(&#39;.led-container&#39;);
document.querySelector(&#39;.led-slider&#39;).addEventListener(&#39;input&#39;, (e) =&gt; {
container.style.setProperty(&#39;--position&#39;, `${e.target.value}%`);
});

huangapple
  • 本文由 发表于 2023年6月15日 19:49:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定