简体   繁体   English

删除 div 部分下方的空白区域

[英]Remove white space below div section

I have a div that includes two images that when each is clicked they reveal some text.我有一个包含两个图像的 div,当单击每个图像时,它们会显示一些文本。

 $('span.a, span.b').click(function() { if (!$(this).hasClass('active')) { $('span.a, span.b').removeClass('active'); $(this).addClass('active'); $('div.a, div.b').toggle(); } $('div.a, div.b').css("visibility","visible") });
 div.a, div.b { visibility:hidden; } .footer { font-family:'Arial'; font-size:13px; background-color:#000; color:#fff; text-align:center; padding:20px; }
 <div style="background-color:#fff;"><center><span class="a active"> <img src="http://cornerstoneparking.portfolio.com.au/wp-content/uploads/2016/10/Customers.jpg" width="200"> </span> <span class="b"> <img src="http://cornerstoneparking.portfolio.com.au/wp-content/uploads/2016/12/Landlords.jpg" width="200"></span> <div class="a">Cornerstone Parking provides value for money parking in Brisbane's CBD and surrounding suburbs. Our turn up and park rates (ie no need to pre-book) are often cheaper than other car park's online discount rates and you can always be sure of getting a bay in a Cornerstone car park. Our convenient and centrally located CBD car parks are run by our friendly staff and are predominantly located in the Adelaide Street, Ann Street and Creek Street areas. Our car parks offer discounted parking in large bays with ample height clearance. We offer hourly (visitor) parking as well as monthly parking, early bird parking and motorbike parking in most of our car parks.</div> <div class="b" style="display:none">Cornerstone Parking provides a high quality, professional and technology driven car park management service. A part of the Cornerstone Group, our property development and management heritage provides us with a true appreciation of landlord issues. Our parent company backing means that Cornerstone Parking has the appetite and ability to participate in larger parking projects, including the development of new car parks. We provide owners, investors and developers with our car park management, advisory and consultation services.</div> </center> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script></div> <div class="footer">Footer content </div>

I have two questions: I need the large white space below the two images in the first div to be minimised - can this be done?我有两个问题:我需要最小化第一个 div 中两个图像下方的大空白 - 可以这样做吗?

Also is it possible for a Javascript code that will hide the text again when the images are clicked again?当再次单击图像时,Javascript 代码是否可能再次隐藏文本?

Thanks!谢谢!

The extra space comes from using visibility: hidden;额外的空间来自使用visibility: hidden; which will leave the layout of the hidden content on the page, but hide it visually (so it's invisible).这将在页面上留下隐藏内容的布局,但在视觉上隐藏它(所以它是不可见的)。 Use display: none;使用display: none; instead to remove the content's layout from the page as if it were never there.而是从页面中删除内容的布局,就好像它从未存在过一样。

Then you can simplify your jQuery and just toggle a class to designate active/inactive images, then use sibling selectors in CSS to show the active content.然后您可以简化您的 jQuery,只需切换一个类来指定活动/非活动图像,然后在 CSS 中使用同级选择器来显示活动内容。

 $('span.a, span.b').click(function() { $(this).siblings().removeClass('active'); $(this).toggleClass('active'); });
 div.a, div.b { display: none; } .a.active ~ .a, .b.active ~ .b { display: block; } .footer { font-family:'Arial'; font-size:13px; background-color:#000; color:#fff; text-align:center; padding:20px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div style="background-color:#fff;"> <center><span class="a active"> <img src="http://cornerstoneparking.portfolio.com.au/wp-content/uploads/2016/10/Customers.jpg" width="200"> </span> <span class="b"> <img src="http://cornerstoneparking.portfolio.com.au/wp-content/uploads/2016/12/Landlords.jpg" width="200"></span> <div class="a">Cornerstone Parking provides value for money parking in Brisbane's CBD and surrounding suburbs. Our turn up and park rates (ie no need to pre-book) are often cheaper than other car park's online discount rates and you can always be sure of getting a bay in a Cornerstone car park. Our convenient and centrally located CBD car parks are run by our friendly staff and are predominantly located in the Adelaide Street, Ann Street and Creek Street areas. Our car parks offer discounted parking in large bays with ample height clearance. We offer hourly (visitor) parking as well as monthly parking, early bird parking and motorbike parking in most of our car parks.</div> <div class="b">Cornerstone Parking provides a high quality, professional and technology driven car park management service. A part of the Cornerstone Group, our property development and management heritage provides us with a true appreciation of landlord issues. Our parent company backing means that Cornerstone Parking has the appetite and ability to participate in larger parking projects, including the development of new car parks. We provide owners, investors and developers with our car park management, advisory and consultation services.</div> </center> </div> <div class="footer">Footer content </div>

Your JavaScript function only checks if the class is not active with您的 JavaScript 函数仅检查该类是否处于活动状态

if (!$(this).hasClass('active'))

You should write an else statement to toggle the visibility.您应该编写一个 else 语句来切换可见性。

The white space is a styling issue.空白是一个样式问题。 You should edit the margin/padding in CSS.您应该在 CSS 中编辑边距/填充。

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

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