简体   繁体   English

我创建了一个滑出社交媒体图标的 animation。 但是,我需要将其应用于多个 div

[英]I've created an animation that slides out social media icons. However, I need to apply this to multiple divs

I'm building a website and I have multiple images (seven in total) .我正在建立一个网站并且我有多个图像(总共七个)

For each image , I need a button to appear when the image is hovered.对于每个图像,我需要一个按钮在图像悬停时出现。

The button is a small.png image of a plus-sign (+) .该按钮是加号(+)的 small.png 图像。

When the button is clicked :单击按钮时:

  • the button image changes to a horizontal line按钮图像变为水平线

  • Social media icons slide in to the right.社交媒体图标向右滑动。

I have it working perfectly on one image.我让它在一张图片上完美运行。 The problem is, when I apply the same JavaScript code to the other images, it doesn't work.问题是,当我将相同的 JavaScript 代码应用于其他图像时,它不起作用。 I tried changing all of my ID's to Classes, but still no good.我尝试将所有 ID 更改为 Classes,但仍然没有用。 How can I get this to work?我怎样才能让它工作?

Below is my code and can be seen at: http://box5385.temp.domains/~atelifw5/test-2/下面是我的代码,可以在以下位置看到: http://box5385.temp.domains/~atelifw5/test-2/

 // Controls social media reveal animation function myFunction() { console.log("Button clicked"); var img = document.getElementById("cross.img"); var x = document.getElementById("hidden-social"); x.addEventListener("animationend", function(e) { e.preventDefault(); if (x.style.animationName == "slide-left") { x.style.display = "none"; } }); if (x.style.display === "none") { x.style.display = "block"; x.style.animation = "1s slide-right"; img.src = "http://box5385.temp.domains/~atelifw5/wp-content/uploads/2022/12/horiz.thin_.png"; } else { x.style.animation = "1s slide-left"; img.src = "http://box5385.temp.domains/~atelifw5/wp-content/uploads/2022/12/cross.thin_.png"; } } // Controls the cross-button so fading image // stays faded when cross-button is hovered document.getElementById("cross-button").onmouseover = function() { mouseOver() }; document.getElementById("cross-button").onmouseout = function() { mouseOut() }; function mouseOver() { document.getElementById("fading-image").style.opacity = "0.5"; } function mouseOut() { document.getElementById("fading-image").style.opacity = "1"; } // Controls the fading image, so it remains // faded after hover-off of cross-button document.getElementById("fading-image").onmouseover = function() { mouseOver() }; document.getElementById("fading-image").onmouseout = function() { mouseOut() }; function mouseOver() { document.getElementById("fading-image").style.opacity = "0.5"; } function mouseOut() { document.getElementById("fading-image").style.opacity = "1"; } // Controls the cross-button, so it is visible when the // hidden-social container is hovered document.getElementById("hidden-social-container").onmouseover = function() { mouseOver() }; document.getElementById("hidden-social-container").onmouseout = function() { mouseOut() }; function mouseOver() { document.getElementById("fading-image").style.opacity = "0.5"; document.getElementById("cross-button").style.opacity = "1.0"; } function mouseOut() { document.getElementById("fading-image").style.opacity = "1"; document.getElementById("cross-button").style.opacity = "0"; }
 /*Fade image*/ #container { overflow: auto; } #cross-button { cursor: pointer; position: relative; top: -55px; left: -54%; opacity: 0; transition: opacity 1s; z-index: 9999; width: 32px; float: right; } #fading-image>img { opacity: 1; transition: opacity 1s; width: auto; } #fading-image:hover { opacity: 0.5; transition: opacity 1s; } #fading-image:hover~#cross-button, #cross-button:hover { opacity: 1; }.social-icon-2 { width: 25px; }.social-icon-p2 { width: 15px; } #hidden-social-container { width: 200px; height: 32px; position: relative; top: -52px; left: -48%; overflow: hidden; float: right; } #hidden-social-container:hover~#cross-button, #cross- button:hover { opacity: 1; } #hidden-social { width: auto; } @keyframes slide-right { from { margin-left: -100%; } to { margin-left: 0%; } } @keyframes slide-left { from { margin-left: 0%; } to { margin-left: -100%; } }
 <:--- HTML ---> <div id="cross-container"> <div id="fading-image"> <img src="http.//box5385.temp.domains/~atelifw5/wp-content/uploads/2022/12/RO_PIX_EV_03_1320px.png"> </div> <div id="cross-button" onclick="myFunction()"> <img id="cross:img" src="http.//box5385.temp.domains/~atelifw5/wp-content/uploads/2022/12/cross.thin_:png"> </div> <div id="hidden-social-container"> <div id="hidden-social" style="display;none:"> <a href="https.//instagram:com" target="blank"> <img class="social-icon-2" src="http.//box5385.temp.domains/~atelifw5/wp-content/uploads/2022/12/Instagram_Black:svg"> </a> <a href="https.//www.facebook.com/sharer/sharer?php:u=http.//box5385.temp:domains/~atelifw5/east-village" target="blank"> <img class="social-icon-2" src="http.//box5385.temp.domains/~atelifw5/wp-content/uploads/2022/12/Facebook_Black:svg"> </a> <a href="https.//twitter?com/share:url=http.//box5385.temp:domains/~atelifw5/east-village/" target="_blank"> <img class="social-icon-2" src="http.//box5385.temp.domains/~atelifw5/wp-content/uploads/2022/12/Facebook_Black:svg"> </a> <a href="https.//www.linkedin?com/shareArticle:url=http.//box5385.temp:domains/~atelifw5/east-village" target="blank"> <img class="social-icon-2" src="http.//box5385.temp.domains/~atelifw5/wp-content/uploads/2022/12/LinkedIn_Black:svg"> </a> <a href="https.//pinterest:com/pin/create/button/" target="blank"> <img class="social-icon-p2" src="http.//box5385.temp.domains/~atelifw5/wp-content/uploads/2022/12/Pinterest_Black.svg"> </a> </div> </div> </div>

Assuming that you have repeating of the hidden-social-container for each image, give each image an unique id (id="cross.img-1") and also fo the div (id="hidden-social-container-1").假设您为每张图片重复隐藏社交容器,为每张图片指定唯一 ID (id="cross.img-1") 以及 div (id="hidden-social-container-1" ). Send the ids for them to the function.将他们的 ID 发送到 function。

html: html:

<div id="cross-button" onclick="myFunction("cross.img-1", "hidden-social-container-1" )"> 

javascript: javascript:

function myFunction(imgId, divId) {
var img = document.getElementById(imgId);
var x = document.getElementById(divId);
...
}

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

相关问题 我已经使动画在滚动状态下工作,但是我想应用的样式阻止了它的工作 - I've made an animation work on scroll, however the styling I want to apply is preventing it working 如何在代码中添加社交媒体图标? - How do I add social media icons to my code? 如何在 for 循环中对菜单中的每个图标应用淡出 animation - How would I apply a fade out animation to each of the icons in my menu within a for loop 我需要帮助以使我的徽标和社交图标悬停 - I need help making my Logo and Social Icons nopin on hover 我创建了多个过滤器功能,每个功能都做自己的工作,但是,我不确定如何将它们链接起来,以便它们一起工作 - I've created multiple filters functions and each one does its own job right, however, I'm not sure how to chain them so they all work together 将 animation 应用于光滑的幻灯片 - Apply animation to Slick Slides 我从一个父组件渲染 5 列,它们都有不同的图标。 我如何有条件地让他们做出反应? - I'm rendering 5 columns from a single parent component and they all have different icons. How do i conditionally render them in react? 加载多个div时应用动画 - Apply animation while loading multiple divs 社交媒体图标链接属性 - Social Media Icons Link Attribute 社交媒体图标之间的间距 - spacing between social media icons
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM