简体   繁体   English

Javascript和W3CSS奇怪的图像行为

[英]Javascript and W3CSS strange image behaviour

Using the W3Schools static slideshow CSS (and W3CSS): https://www.w3schools.com/w3css/w3css_slideshow.asp 使用W3Schools静态幻灯片CSS(和W3CSS): https ://www.w3schools.com/w3css/w3css_slideshow.asp

However, my images are all different heights and widths, so I want to set them to be all the same height based on the size of the viewport, so I have modified the javascript for the slideshow (see below, 566 and 1280 are the dimensions of the shortest image), however, when using this javascript the slideshow comes out with 0 height, yet when reporting the "height" variable it comes out at the correct (non-zero) value. 但是,我的图像的高度和宽度都不同,因此我想根据视口的大小将它们设置为相同的高度,因此我修改了幻灯片的javascript(请参见下文,尺寸为566和1280)最短图像的),但是,当使用此javascript时,幻灯片的高度为0,但是报告“ height”变量时,其正确(非零)值。

<script>
var slideIndex = 1;

showDivs(slideIndex);

function plusDivs(n) {
    showDivs(slideIndex += n);
}

function showDivs(n) {
    var i;
    var x = document.getElementsByClassName("Gallery");
    if (n > x.length) {slideIndex = 1} 
    if (n < 1) {slideIndex = x.length} ;
    for (i = 0; i < x.length; i++) {
        x[i].style.display = "none"; 
    }
    x[slideIndex-1].style.display = "block"; 
    var height = (document.documentElement.clientWidth*566/1280);
    x[slideIndex-1].height = height+"px";
}
</script>

Your last line is incorrect. 您的最后一行不正确。 It should be: 它应该是:

x[slideIndex-1].style.height = height+"px";

A simple way of equalising the height of all images is to use 'vh' units in the CSS. 均衡所有图像高度的一种简单方法是在CSS中使用“ vh”单位。 Then it does not need any Javascript to detect or set any heights. 然后,它不需要任何Javascript即可检测或设置任何高度。 If all images of class 'mySlides' need to be, say 50% of the viewport height: 如果需要将“ mySlides”类的所有图像作为视口高度的50%:

img.mySlides { 
  height: 50vh; width: auto; 
} 

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

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