简体   繁体   English

触发另一个元素的淡入/淡出会导致在切换触发元素时淡入淡出粘贴

[英]Triggering a fade in/fade out from another element causes the fading element to stick when switching trigger elements

I asked a question earlier about fading an element in and out which was answered with the solution I will post later on, however I left out some information that proves to be more relevant than I originally realized. 之前,我问了一个有关元素淡入淡出的问题,稍后我将在解决方案中对此进行了回答,但是我遗漏了一些信息,这些信息比我最初意识到的要重要得多。

I am trying to fade an element in on the hover of a separate trigger element and out when the mouse leaves that same trigger element. 我试图在单独的触发元素的悬停上淡入一个元素,并在鼠标离开相同的触发元素时淡出一个元素。 currently if I move from one trigger element to another trigger element the first element doesn't fade out. 当前,如果我从一个触发元素移动到另一个触发元素,则第一个元素不会淡出。

I have tried a number of solutions including the following two jsfiddles which handle the fading fine but still have the issue I was having occuring when switching trigger elements. 我已经尝试了多种解决方案,包括以下两个jsfiddles,它们可以很好地处理褪色,但是仍然存在切换触发器元素时发生的问题。

Here is the current mock up of code I'm using: 这是我正在使用的代码的当前模型:

<img id="project1-img" src="https://i.imgur.com/ZEDaxij.jpg" style="height: 100px; width: 100px; opacity: 0;">
<img id="project2-img" src="https://i.imgur.com/ZEDaxij.jpg" style="height: 100px; width: 100px; opacity: 0;">
<img id="project3-img" src="https://i.imgur.com/ZEDaxij.jpg" style="height: 100px; width: 100px; opacity: 0;">

<span id="trigger1">
111111111111
</span>

<span id="trigger2">
22222222222
</span >

<span id="trigger3">
33333333333
</span>
let state = "disappeared"; // disappeared, appearing, appeared, disappearing
let opacity = 0; // Current raw opacity value
let fadeInc = 0.05; // Amount to fade each frame

let findElementToFade = (imageId) => {
    let elem = document.getElementById(imageId);
        tryDoFade(elem);
}

let tryDoFade = elem =>
  requestAnimationFrame(() => {
    //console.log("called with " + imageId);
    //let state = "disappeared"; // disappeared, appearing, appeared, disappearing

    let changed = false;
    if (state === "appearing" && opacity < 1)
      opacity = Math.min(1, opacity + fadeInc);
    else if (state === "disappearing" && opacity > 0)
      opacity = Math.max(0, opacity - fadeInc);

    elem.style.opacity = opacity;

    // Enter a rest state if opacity maxes out in either direction
    if (opacity === 0) state = "disappeared";
    else if (opacity === 1) state = "appeared";

    // Keep animating if not in a rest state
    if (state === "disappearing" || state === "appearing") tryDoFade(elem);
  });

let e = document.getElementById('trigger1');
e.onmouseover=() =>(state = "appearing") && findElementToFade(`project3-img`)
e.onmouseout=() =>(state = "disappearing") && findElementToFade(`project3-img`)

let f = document.getElementById('trigger2');
f.onmouseover=() =>(state = "appearing") && findElementToFade(`project2-img`)
f.onmouseout=() =>(state = "disappearing") && findElementToFade(`project2-img`)

let g = document.getElementById('trigger3');
g.onmouseover=() =>(state = "appearing") && findElementToFade(`project1-img`)
g.onmouseout=() =>(state = "disappearing") && findElementToFade(`project1-img`)

Here are the JsFiddles I've been playing with: https://jsfiddle.net/dm9cgysL/1/ 这是我一直在玩的JsFiddles: https ://jsfiddle.net/dm9cgysL/1/

This is the solution to my last question which doesn't work in my particular case: https://jsfiddle.net/03sgm1ot/ 这是我的最后一个问题的解决方案,在我的特定情况下不起作用: https : //jsfiddle.net/03sgm1ot/

Here is a jsfiddle that is more accurate to what I am trying to do: https://jsfiddle.net/dm9cgysL/6/ 这是一个jsfiddle,它与我要执行的操作更加准确: https ://jsfiddle.net/dm9cgysL/6/

I expect for each of the elements to fade in and out independently as soon as the mouse enters/exits the trigger elements, currently if I go from one trigger element to the next it causes the previous fade to stop in its tracks leaving the element on the page. 我希望每个元素在鼠标进入/退出触发元素后都会独立淡入和淡出,目前,如果我从一个触发元素转到下一个触发元素,它将导致前一个淡入淡出停止在其轨迹中,从而使该元素保持打开状态这一页。

RESOLVED: I modified the answer I marked as the answer to fit my needs here is what I did: 解决:我修改了我标记为答案的答案,以满足我在这里所做的事情:

CSS: CSS:

img.transition {
  transition: opacity 0.25s;
  -moz-transition: opacity 0.25s;
  -webkit-transition: opacity 0.25s;
  opacity: 0;
}

img.transition.visible {
  opacity: 1;
}

The images I used have a class name of transition : 我使用的图像的类名是transition

<img id="project1-img" src="/static/media/mtg.4c420286.png" class="ui fluid image transition" >

My trigger elements have the following mouse events: 我的触发器元素具有以下鼠标事件:

onMouseOver={() => handleTransition(`project1-img`)}
onMouseOut={() => handleTransition(`project1-img`)}

And my handleTransition function is as follows: 我的handleTransition函数如下:

const handleTransition = id => {
  let target = document.getElementById(id);
  if (target.classList.contains("visible")) {
    target.classList.remove("visible");
  } else {
    target.classList.add("visible");
  }
};

This is a much cleaner solution to the issue I was having, I didn't realize that css had built in transitions like this and I'm going to have to look into them further. 这是解决我遇到的问题的一种更为简洁的解决方案,我没有意识到css内置了这样的转换,因此我将不得不对其进行进一步研究。 Thanks to everyone who took a look and thanks to GammaGames for the solution that got me here. 感谢大家的光临,也感谢GammaGames为我带来的解决方案。

Learning css would really help with this case, especially since you're modifying the element's css manually with the style attribute anyway. 学习css确实可以解决这种情况,特别是因为无论如何您都使用style属性手动修改元素的css。 I'm sure you could use data attributes to store timeout ids or something hacky, but for a simple solution I: 我确定您可以使用data属性来存储超时ID或一些骇客的东西,但是对于一个简单的解决方案,我:

  1. Add a data-target attribute to each <span> that I want to listen to data-target属性添加到我想听的每个<span>
  2. On mouseover, add a visible class to the image with the matching target id 鼠标悬停时,将具有visible目标ID的visible类添加到图像
  3. On mouseout, remove the visible class to the image with the matching target id 移出鼠标时,将具有visible目标ID的visible类移除到图像

img tags with the transition class have 0 opacity by default and transition with 0.25s time. 默认情况下,带有transition类的img标签的不透明度为0,过渡时间为0.25s。 The visible class makes the opacity into 1, and the element automatically transitions. visible类使不透明度变为1,元素自动过渡。 You can read more about transitions here . 您可以在此处阅读有关过渡的更多信息。

 $(".trigger").on("mouseover", function() { let target = $(this).data("target"); $(`#${target}`).addClass("visible"); }); $(".trigger").on("mouseout", function() { let target = $(this).data("target"); $(`#${target}`).removeClass("visible"); }); 
 img.transition { transition: opacity 0.25s; -moz-transition: opacity 0.25s; -webkit-transition: opacity 0.25s; opacity: 0; } img.transition.visible { opacity: 1; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <img class="transition" id="project1-img" src="https://picsum.photos/100"> <img class="transition" id="project2-img" src="https://picsum.photos/100"> <img class="transition" id="project3-img" src="https://picsum.photos/100"> </div> <div> <span class="trigger" data-target="project3-img"> 111111111111 </span> <span class="trigger" data-target="project2-img"> 22222222222 </span > <span class="trigger" data-target="project1-img"> 33333333333 </span> </div> 

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

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