简体   繁体   English

如何使用jQuery根据浏览器高度调整大小添加或删除类?

[英]How to add or remove classes based on browser height resize using jquery?

I would like to know if there's a way to add or remove classes based on the browser's height. 我想知道是否有一种基于浏览器高度的添加或删除类的方法。 Right now, I am comparing a div against the browser height and adding a class to it if the browser's height is higher than the div height by doing this: 现在,我正在将div与浏览器的高度进行比较,如果浏览器的高度高于div的高度,则向其添加一个类,方法是:

if (($(window).height()) > $(".sidebar").height()) {
    $("#widget-area").addClass("fixed");
} else {

}

This works since the class is being added when the condition is met. 这是可行的,因为在满足条件时将添加类。 The problem is that if the user resizes the browser's height, the class that was added will keep added even if the condition is not met anymore. 问题是,如果用户调整浏览器的高度,即使不再满足条件,添加的类也将继续添加。

Is there a way to listen to the browser's height and add or remove this class no matter if the browser is resized later on? 有没有一种方法可以监听浏览器的高度并添加或删除此类,而不管以后是否重新调整浏览器的大小?

EDIT: 编辑:

I know a lot of you might suggest doing this by media queries but I need this to be done using jQuery. 我知道很多人可能建议通过媒体查询来做到这一点,但是我需要使用jQuery来做到这一点。

I have added the window on resize function as suggested. 我已经按照建议在调整大小功能上添加了窗口。 The problem is that the script will only run if the browser is resized. 问题在于该脚本仅在调整浏览器大小后才能运行。 I need it to run as soon as the document is ready and if the browser is resized as well. 我需要它在文档准备好后以及浏览器大小调整后立即运行。 Here is my code: 这是我的代码:

$(window).on('resize', function(){
    if (($(window).height()) > $(".sidebar").height()) {
        $("#widget-area").addClass("fixed");
    } else {

    }
});

The CSS media queries is a good way to do this work. CSS 媒体查询是完成此工作的好方法。 But if you want to use jQuery, you should run the code when window resize. 但是,如果要使用jQuery,则应在调整窗口大小时运行代码。

$(window).resize(function(){
  if ($(window).height() > $(".sidebar").height())
    $("#widget-area").addClass("fixed");
  else
    $("#widget-area").removeClass("fixed");
});

Also if you want to run code when page load, use .on() and add two event handler to it. 另外,如果要在页面加载时运行代码,请使用.on()并向其中添加两个事件处理程序。

$(window).on('load resize', function(){
  if ($(window).height() > $(".sidebar").height())
    $("#widget-area").addClass("fixed");
  else
    $("#widget-area").removeClass("fixed");
});

See code result in demo 演示中查看代码结果

Solution via javascript: 通过javascript解决方案:

If .sidebar has a dynamic height, here is one approach using javascript: 如果.sidebar具有动态高度,这是使用javascript的一种方法:

function updateWidgetAreaClassList() {
    var widgetArea = document.getElementById('widget-area');
    var sideBar = document.getElementsByClassName('sidebar')[0];

    if (window.innerHeight > sideBar.offsetHeight) {
        widgetArea.classList.add('fixed');
    }

    else {
        widgetArea.classList.remove('fixed');
    }
}

window.addEventListener('resize', updateWidgetAreaClassList, false);

Solution via CSS @media query: 通过CSS @media查询的解决方案:

If .sidebar has a fixed height of 400px (for example), the CSS @media query would be the following: 如果.sidebar的固定高度为400px (例如),则CSS @media查询将为以下内容:

@media screen and (min-height: 401px ) {

#widget-area { [... STYLES HERE...] }

}

Create a function for manipulating your dom in your case add and remove classes. 创建一个函数来处理您的dom添加和删除类。 And call that funciton in document.ready function and window.resize function. 并在document.ready函数和window.resize函数中调用该函数。 See below a working example 参见下面的工作示例

http://codepen.io/harishgadiya/pen/VpNXmB http://codepen.io/harishgadiya/pen/VpNXmB

HTML HTML

<div id="wrapper"></div>

css CSS

#wrapper{
  height:300px;
  width:300px;
  background:#999
}
.red{
  background:red !important;

}

JS JS

function resize(){
  var windowHeight = $(window).height();
  var wrapperHeight = $('#wrapper').height();
  console.log(windowHeight , wrapperHeight)
  if(windowHeight > wrapperHeight) $('#wrapper').addClass("red");
  else $('#wrapper').removeClass("red");
}
$(document).ready(function(){
  resize()
});

$(window).resize(resize)

Though I've seen in an answer above $(window).on('load resize', function(){.. which should do the job you can also do something like this: 尽管我在$(window).on('load resize', function(){..上面的答案中看到过,这应该可以完成工作,但您也可以执行以下操作:

 var handleResize = function(){ if (($(window).height()) > $(".sidebar").height()) { $("#widget-area").addClass("fixed"); } else { //some other thing } } $(document).ready(function(){ handleResize(); }) $(window).on('resize', function(){ handleResize(); }); 

Go for media query
@media only screen and (max-height: 500px) {

.fixed :100px;



}

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

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