简体   繁体   English

如何通过将光标传递到DIV上来修改DIV内部并进行图像处理

[英]How can I modify and image inside a DIV by passing the cursor over the DIV

I'm starting out at coding and I'm stuck in a code that I'm working on. 我开始编写代码而且我遇到了正在处理的代码。 First I pass the mouse inside my DIV container called "Article" and everything inside change its opacity. 首先,我将鼠标放在名为“Article”的DIV容器中,内部的所有内容都会改变其不透明度。 I want just the image that is inside's opacity changed, so everywhere inside the DIV the opacity of the image should be 1. Once the mouse is out the opacity of the image should become 0.75. 我只想改变内部不透明度的图像,所以在DIV内部的任何地方,图像的不透明度应为1.一旦鼠标移出,图像的不透明度应该变为0.75。 I tried multiple codes but the opacity changed for every element inside the DIV. 我尝试了多个代码但是DIV内的每个元素的不透明度都发生了变化。 Hope you could help me. 希望你能帮助我。

I tried to change document . 我试图改变文件。 getElementsByTagName ('article') [i] by document.getElementsByTagName('img')[i] but it just changed the image opacity when mouse is over the image. 通过document.getElementsByTagName('img')[i]获取getElementsByTagName('article')[i]但它只是在鼠标悬停在图像上时更改了图像的不透明度。 I tried document.getElementsByTagName('article')[i].getElementsByTagName('img')[i] same as before. 我和以前一样尝试了document.getElementsByTagName('article')[i] .getElementsByTagName('img')[i]。 I tried to change this.style by img.style nothing happened... 我试图通过img.style改变this.style什么都没发生...


for (var i = 0; i<document.querySelectorAll('article').length;i++)
document.getElementsByTagName('article')[i].onmouseover=function(){
this.style.opacity = 1;
}
for (var i = 0; i<document.querySelectorAll('article').length;i++)
document.getElementsByTagName('article')[i].onmouseleave=function(){
this.style.opacity = 0.75;
}

Hope that when I pass over an article the image opacity changes. 希望当我通过一篇文章时,图像不透明度会发生变化。 Thanks, you all an have a great year! 谢谢,你们都有一个美好的一年!

Try using querySelectorAll('article img') instead of getElementsByTagName. 尝试使用querySelectorAll('article img')而不是getElementsByTagName。 This will only affect img inside article. 这只会影响img里面的文章。

Thanks Guys in fact at the end I change to css and it works perfectly sorry for being such a noobie and say that this doesn't work. 感谢伙计们最后我改为css,并且非常抱歉这样一个noobie并说这不起作用。 I named my Article (DIV) "Dad" and my image "child". 我将我的文章(DIV)命名为“爸爸”,将我的形象命名为“孩子”。

OMG it was to simple and I complicate my life by a lot. OMG很简单,我的生活很复杂。 This is my final code: 这是我的最终代码:

.Dad:hover .Child {
  opacity: 1;
}

I'm not sure what are you asking for, but check this: 我不确定你要求的是什么,但检查一下:

HTML HTML

<div class="container">
  <h3>this is container</h3>
  <img src="https://picsum.photos/200/300/?123" alt="">
  <img src="https://picsum.photos/200/300/?14" alt="">
  <img src="https://picsum.photos/200/300/?15" alt="">
</div>

CSS CSS

.container{
    background: #d87300;
}

.half-transparent{
    opacity: 0.5;
}

JS JS

const images = [...document.querySelectorAll('.container img')]
const container = document.querySelector('.container')

container.addEventListener('mouseenter', ()=>{
    images.forEach(element => {
        element.classList.add('half-transparent')
    })
})

container.addEventListener('mouseleave', ()=>{
    images.forEach(element => {
        element.classList.remove('half-transparent')
    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM