简体   繁体   English

如果它与另一个div重叠,如何隐藏div

[英]How to hide a div if it overlaps another div

Is this CSS or javascript? 这是CSS还是javascript? I just need the div to change to display:none if it comes within say 20px of another div. 我只需要将div更改为显示:如果它在另一个div的20px内,则为none。 Thanks 谢谢

I would try using jquery to check the distance between objects. 我会尝试使用jquery来检查对象之间的距离。 Look at this post 看看这篇文章

Get relative position between 2 DOM elements using JavaScript 使用JavaScript获取2个DOM元素之间的相对位置

Try this https://github.com/brandonaaron/jquery-overlaps 试试这个https://github.com/brandonaaron/jquery-overlaps

//Listen to the event that will be triggered on window resize:
window.onresize = function(event) 
{
    // test if one element overlaps another
    if($('#div1').overlaps('#div2'))
    {
        //Do stuff, like hide one of the overlapping divs
        $('#div1').hide();
    }
}

You can add an event handler that gets fired when the window is resized. 您可以添加在调整窗口大小时触发的事件处理程序。 You could do this with javascript or jquery. 你可以用javascript或jquery做到这一点。 jquery makes it easy: jquery让它变得简单:

window.onresize = function(event) {
var h=$(window).height();
var w=$(window).width();

if(h<400 && w < 300){
 //hide divs
 $('#yourdivid1').hide();
}


}

Hope this helps 希望这可以帮助

Based on your comment: 根据您的评论:

Yes it is so that if the user makes their browser window small my site does not look crowded 是的,如果用户使浏览器窗口变小,我的网站看起来并不拥挤

Instead of answering the question you asked, Here's an answer to the question you didn't ask: 而不是回答你问的问题,这是你没有问过的问题的答案:

How to resize/position/cssify page elements based on browser size? 如何根据浏览器大小调整页面元素的大小/位置/ cssify?

There is a new-ish application of css and javascript called Responsive Web Design . 有一个名为响应式网页设计的css和javascript的新应用。 Responsive Design allows you to specify different css rules to apply based on different elements. 响应式设计允许您根据不同的元素指定要应用的不同css规则。 For a great example of this technique, resize your browser around on The Boston Globe 's website. 有关此技术的一个很好的例子,请在波士顿环球报的网站上调整浏览器大小。 They just integrated this technique sometime this week. 他们本周在某个时候整合了这项技术。

Here's an example of the css that would implement this: 这是一个实现这个的css的例子:

@media screen and (min-width: 480px) {

  .content {
    float: left;
  }

  .social_icons {
    display: none
  }

  // and so on...

}

example from http://thinkvitamin.com/design/beginners-guide-to-responsive-web-design/ 来自http://thinkvitamin.com/design/beginners-guide-to-responsive-web-design/的示例

Here is a boilerplate to get you going. 这是一个让你前进的样板

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

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