简体   繁体   English

如何将这两个功能合二为一?

[英]How can I combine these two functions into one?

I have a div with image and a div with text.我有一个带图像的 div 和一个带文本的 div。 When I hover over image, it changes the grayscale and hovering over text changes opacity.当我在图像上 hover 时,它会改变灰度,并且将鼠标悬停在文本上会改变不透明度。 How can I combine the js code so that hovering over image or text with change both grayscale of image and opacity of text?如何组合 js 代码,以便将鼠标悬停在图像或文本上,同时更改图像的灰度和文本的不透明度?

      <div class="images">
        <img src="/photographs/FootballTeam.jpg" />
        <div class="image_description">The team. 14/07/17</div>
      </div>
var images = document.querySelectorAll("#photos > .images > img");
for (var i = 0; i < images.length; i++) {
  images[i].style.transitionDuration = "0.1s";
  images[i].addEventListener("mouseenter", function() {
    if (this.style.filter === "grayscale(100%)") {
      this.style.filter = "grayscale(0%)";
    } else {
      this.style.filter = "grayscale(100%)";
    }
  });
}

var images = document.querySelectorAll(
  "#photos > .images > .image_description"
);
for (var i = 0; i < images.length; i++) {
  images[i].style.transitionDuration = "0.1s";
  images[i].addEventListener("mouseenter", function() {
    if (this.style.opacity === "0") {
      this.style.opacity = "1";
    } else {
      this.style.opacity = "0";
    }
  });
}

I hope you have same no of images and the text corresponding to them here.我希望你在这里有相同的图像和与它们相对应的文本。 So, just try the below code and revert back -所以,只需尝试下面的代码并恢复 -

var images = document.querySelectorAll("#photos > .images > img");
 var imagesWithDesc = document.querySelectorAll(
  "#photos > .images > .image_description"
);

for (var i = 0; i < images.length; i++) {
  images[i].style.transitionDuration = "0.1s";
  images[i].addEventListener("mouseenter", function () {
    if (this.style.filter === "grayscale(100%)") {
      this.style.filter = "grayscale(0%)";
    } else {
      this.style.filter = "grayscale(100%)";
    }
  });
  imagesWithDesc[i].style.transitionDuration = "0.1s";
  imagesWithDesc[i].addEventListener("mouseenter", function () {
    if (this.style.opacity === "0") {
      this.style.opacity = "1";
    } else {
      this.style.opacity = "0";
    }
  });
}

Here is my answer:这是我的答案:

//Get all images containers
var images = document.querySelectorAll("#photos > .images");

//Loop every container
for (var i = 0; i < images.length; i++) {
    //set variables for code readability
    let container = images[i];
    let description = self.querySelectorAll(".image_description")[0];

    //set transition durations
    container.style.transitionDuration = "1s";
    description.style.transitionDuration = "1s";

    //add mouse events
    container.addEventListener("mouseenter", function() {
        container.style.filter = "grayscale(100%)";
        description.style.opacity = "0";
    });
    container.addEventListener("mouseleave", function() {
        container.style.filter = "grayscale(0%)";
        description.style.opacity = "1";
    });
}

I suggest following changes to your code:我建议对您的代码进行以下更改:

var imageContainers = document.querySelectorAll("#photos > .images");
imageContainers.forEach(imageContainer => {
   imageContainer.addEventListener('mouseenter', () => {
      imageContainer.classList.toggle('grayscale')
   });
})

And then, in your css:然后,在您的 css 中:

  #photos .images > * {
    transition: 0.5s;
  }

  #photos .images > img {
      filter: grayscale(0%);
  }

  #photos .images.grayscale > img {
      filter: grayscale(100%);
  }


  #photos .images > .image_description {
      opacity: 0;
  }


  #photos .images.grayscale > .image_description {
      opacity: 1;
  }

This would be better to make using CSS only.仅使用 CSS 会更好。 If you need, you can also attach hover event listener.如果需要,还可以附加 hover 事件监听器。

 .images { position: relative; width: 500px; height: 300px; }.images img { width: 100%; height: 100%; /* Your properties */ transition: .25s; /*(.1s is too fast)*/ }.images.image_description { /* Get it to place */ position: absolute; top: 0; left: 0; width: 100%; height: 100%; text-align: center; line-height: 300px; /* Some style */ color: white; cursor: pointer; font-family: sans-serif; font-size: 28px; letter-spacing: 3px; text-transform: uppercase; text-shadow: 0px 0px 5px black; /* Your properties */ opacity: 0; transition: .25s; } /* Image hover event */.images:hover img { filter: grayscale(1); } /* Description hover event */.images:hover.image_description { opacity: 1; }
 <div class="images"> <img src="https://natgeo.imgix.net/factsheets/thumbnails/01-balance-of-nature.adapt.jpg"/> <div class="image_description">the nature</div> </div> I'm not owner of the image.

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

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