简体   繁体   English

删除隐藏图像的“空白”

[英]Removing 'white-space' of hidden images

I have a list of images that I would like to hide/show based on which <li> element is clicked. 我有一个图像列表,我想根据单击的<li>元素来隐藏/显示。 I have been able to do this successfully, however, there is still the white space below/above the image that is showing. 我已经能够成功完成此操作,但是,在显示的图像下方/上方仍存在空白区域。 Here is the code I am currently using: 这是我当前正在使用的代码:

HTML HTML

<div class="img-container">
    <img id="img1" src="img/image1.jpg" />
    <img id="img2" src="img/image2.jpg" />
    <img id="img3" src="img/image3.jpg" />
</div>

CSS CSS

.img-container{
    text-align: center;
    visibility: hidden;
    margin-top: 20px;
}

JS JS

var img1 = document.getElementById('img1');
var img2 = document.getElementById('img2');
var img3 = document.getElementById('img3');

("li:nth-child(2)").on('click', function() {
    img1.style.visibility = 'visible';
    img2.style.visibility = 'hidden';
    img3.style.visibility = 'hidden';
});
("li:nth-child(3)").on('click', function(){
    img2.style.visibility = 'visible';
    img1.style.visibility = 'hidden';
    img3.style.visibility = 'hidden';
});
("li:last-child").on('click', function() {
    img3.style.visibility = 'visible';
    img2.style.visibility = 'hidden';
    img1.style.visibility = 'hidden';
});

I have tried playing around with the display: hidden css property paired with .toggle() but cannot seem to quite get it working correctly. 我试着玩display: hidden CSS属性与.toggle()配对,但似乎无法完全正常工作。 I have tried searching for this but cannot find anything on removing the white space the hidden image is holding. 我尝试搜索此内容,但是在删除隐藏图像所保留的空白时找不到任何东西。 Am relatively new to JS/jQuery. 对JS / jQuery来说相对较新。 Thanks. 谢谢。

When visibility is set to hidden , the element is not shown but still takes up space on the page. visibility设置为hidden ,该元素不会显示,但仍会占用页面空间。

When display is set to none , the element is neither shown nor does it take up any space on the page. display设置为none ,既不显示元素也不占用页面上的任何空间。

More often, I usually need: 通常,我通常需要:

display = 'none';

or 要么

display = ''; // to have it show

You could use the css property display and values none and block . 您可以使用css属性display并将值设置为noneblock The display property specifies if/how an element is displayed. display属性指定是否/如何显示元素。

replace for visible img: 替换为可见的img:

img1.style.visibility = 'visible';

for: 对于:

$("#img1").css("display", "block");

And replace for invisible img: 并替换为不可见的img:

img1.style.visibility = 'hidden';

for: 对于:

$("#img1").css("display", "none");

Could use more useful inline-block instead block for lists. 可以使用更有用的inline-block代替列表block

If looking for jQuery solution: 如果要寻找jQuery解决方案:

 var $img1 = $( "#img1" ), $img2 = $( "#img2" ), $img3 = $( "#img3" ); $( "li:nth-child(2)" ).on( "click", function() { $img1.show(); $img2.hide(); $img3.hide(); } ); $( "li:nth-child(3)" ).on( "click", function() { $img1.hide(); $img2.show(); $img3.hide(); } ); $( "li:nth-child(4)" ).on( "click", function() { $img1.hide(); $img2.hide(); $img3.show(); } ); $( "li:last-child" ).on( "click", function() { $img1.show(); $img2.show(); $img3.show(); } ); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <ul style="list-style: none;"> <li>Click a button:</li> <li><button>Show First Photo</button></li> <li><button>Show Second Photo</button></li> <li><button>Show Thrid Photo</button></li> <li><button>Reset</button></li> </ul> <div class="img-container"> <img id="img1" width="300" height="300" src="https://blognumbers.files.wordpress.com/2010/09/1.jpg" /> <img id="img2" width="300" height="300" src="https://blognumbers.files.wordpress.com/2010/09/2.jpg" /> <img id="img3" width="300" height="300" src="https://blognumbers.files.wordpress.com/2010/09/3.jpg" /> </div> 

In addition to using display instead of visibility you could also consider making your JS a little easier to handle. 除了使用display代替visibility您还可以考虑使JS易于处理。 You could simply show the img which has the same index as that of the clicked li . 您可以简单地显示与单击的li具有相同索引的img

EG: 例如:

 $(function() { $(".img-controller li").on("click", function() { var i = $(this).index(); $(".img-container img").hide(); $(".img-container img:eq(" + i + ")").show(); }); $(".img-container img").not(":first").hide(); }); 
 .img-controller { list-style-type: none; padding: 0; } .img-controller li { display: inline-block; background: #eee; padding: 5px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="img-controller"> <li>1</li> <li>2</li> <li>3</li> </ul> <div class="img-container"> <img src="https://placekitten.com/50/50" /> <img src="https://placekitten.com/50/40" /> <img src="https://placekitten.com/50/30" /> </div> 

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

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