[英]How to create array display on function?
I have given four images the same class name and placed them inside a div. 我给了四个图像相同的类名,并将它们放在div中。 Currently the image before is set to display with the four set to not show in CSS. 当前,之前的图像设置为显示,而四个图像则不显示在CSS中。 I am attempting to have the images display onmouseover through Javascript. 我试图通过Javascript在onmouseover上显示图像。 I can do it through CSS but would like to see it through Javascript. 我可以通过CSS来实现,但是希望通过Javascript来实现。 Below is the code: 下面是代码:
HTML HTML
var first = document.getElementById('first'); var Larrow = document.getElementById('arrow1'); var Rarrow = document.getElementById('arrow2'); var slidpics = document.getElementByClassName('crash'); var stop = document.getElementsByClassName('picbac'); var display = 0; first.onmouseover = function() { if (x = 0, x < slidpics.length, x++) { slidpics[display].style.display = 'inline-flex'; display++; } } stop.onmouseout = function() { var y; if (y = 0, y < slidpics.length, y++) { slidpics[display].style.display = 'none'; display++; } }
<div class = "picbac"></div> <img id="first" src="../Pics /January_2019/crash1.jpg" alt="A picture you can't see"> <p id="arrow1"></p> <img id="show1" class="crash" src="../Pics/January_2019/crash2.jpg" alt="Another picture you can't see"> <img id="show2" class="crash" src="../Pics/January_2019/crash3.jpg" alt="You must be blind"> <img id="show3" class="crash" src="../Pics/January_2019/crash4.jpg" alt="Or we're not linking up"> <p id="arrow2"></p> <img id="show4" class="crash" src="../Pics/January_2019/crash5.jpg" alt="Still checkin?">
A good way is keep off the style from the script and to just assign a class to the .picbac
container so as to show the .crash
children with display: inline-flex 一个好的方法是让脚本远离样式,而只为.picbac
容器分配一个类,以便通过display:inline-flex显示.crash
子级。
var first = document.getElementById('first');
var picbac = document.querySelector('.picbac');
first.addEventListener('mouseenter', function() {
picbac.classList.add('show');
});
picbac.addEventListener('mouseleave', function() {
picbac.classList.remove('show');
});
on mouseenter/mouseleave a .show
class is toggled on the .picbac
element, so the CSS could be 在.show
/ mouseleave上,在.picbac
元素上切换了.show
类,因此CSS可以是
.picbac .crash {
display: none;
}
.picbac.show .crash {
display: inline-flex;
}
This approach is possible as long as your .crash
images are children of the .picbac
element and this simplifies a lot the code and it decouples the logic (behaviour and appearance). 只要您的.crash
图像是.picbac
元素的子级,就可以采用这种方法,这可以大大简化代码并使逻辑(行为和外观)脱钩。
Many ways to do this but this is one if you dont mind jquery. 有很多方法可以做到这一点,但是如果您不介意jquery,这就是其中一种。
// show the first image in the set as default $("#wrapper > .content:first-of-type").children("img").show(); // show images on mouse enter $(".content p").on("mouseenter", function(){ $(this).closest(".content").children("img").show(); }); // hide the images on mouse out $(".content p").on("mouseout", function(){ $(this).closest(".content").children("img").hide(); });
.content img{ display: none; } .content p:hover{ cursor: pointer; color: crimson; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script> <div id="wrapper"> <div class="content"> <p>--></p> <img id="first" src="../Pics /January_2019/crash1.jpg" alt="A picture you can't see"> </div> <div class="content"> <p class="arrow">--></p> <img id="show1" class="crash" src="../Pics/January_2019/crash2.jpg" alt="Another picture you can't see"> <img id="show2" class="crash" src="../Pics/January_2019/crash3.jpg" alt="You must be blind"> <img id="show3" class="crash" src="../Pics/January_2019/crash4.jpg" alt="Or we're not linking up"> </div> <div class="content"> <p class="arrow">--></p> <img id="show4" class="crash" src="../Pics/January_2019/crash5.jpg" alt="Still checkin?"> </div> </div>
You don't really need an array to achieve what you're describing. 您实际上不需要数组即可实现您所描述的内容。
Using JQuery with minimal code, I use the arrows to trigger fadeIn()
of the images. 通过最少的代码使用JQuery,我使用箭头触发图像的fadeIn()
。
var imgsDiv = $("#picbac").children(); imgsDiv.on("mouseenter", function() { $(this.children[1]).fadeIn(500); //you can use show() }); imgsDiv.on("mouseout", function() { $(this.children[1]).fadeOut(500); });
.crash { display: none; } [id^=container]{ border: 1px solid lightgrey; margin: 20px; width : 80vw; } p{ font-weight: 900; } img{ width: 50%; height: 50%; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="picbac"> <div id="container1"> <p id="arrow1">Show 1</p> <img id="show1" class="crash" src="https://wallpaperbrowse.com/media/images/soap-bubble-1958650_960_720.jpg" alt="Another picture you can't see"> </div> <div id="container2"> <p id="arrow2">Show 2</p> <img id="show2" class="crash" src="https://www.gettyimages.co.uk/gi-resources/images/500px/983794168.jpg" alt="You must be blind"> </div> <div id="container3"> <p id="arrow3">Show 3</p> <img id="show3" class="crash" src="https://wallpaperbrowse.com/media/images/soap-bubble-1958650_960_720.jpg" alt="Or we're not linking up"> </div> <div id="container4"> <p id="arrow4">Show 4</p> <img id="show4" class="crash" src="https://www.gettyimages.co.uk/gi-resources/images/500px/983794168.jpg" alt="Still checkin?"> </div> </div>
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.