在我的网站上,如果页面首次加载并且已被最大化/全屏显示,div comBrand
具有我要应用的特定CSS属性。检查div是否应用了特定样式
在调整大小事件过程中,我使用.css()
函数将不同属性应用于该元素,以便该div不与其他屏幕项目重叠,但是,实际上不是改变CSS,而是将style属性添加到该div在代码中。有时根据您如何调整页面大小,调整大小功能可能以如下方式结束:使用最大化按钮时,最终会导致特定div的错误位置。我正在检查一些事情以确保不会发生。
但是,我不知道这里的语法是否正确在resize函数期间检查div的样式属性,因为它似乎没有按预期工作。
$(window).resize(function() {
//first check
if ($('#comBrand').css('top') == '155px')
{
//second check to see if the peculiuar event was reached where the div is positioned incorrectly but the window isn't maximized
if (($('#comBrand').attr('style') == 'top: 155px;margin-left: -615px;left: 50%;') && (window.screen.width > window.screen.availWidth))
{
$('#comBrand').css({'top': '155px', 'margin-left': '-615px', 'left': '50%'});
}
else //assume that the page is already maximized and adjust the div accordingly
{
$('#comBrand').css({'top': '141px', 'margin-left': '0', 'left': '0'});
}
}
else //default else, if the CSS of the div is anything other than what was initially checked for, set it back to its default location
{
$('#comBrand').css({'top': '155px', 'margin-left': '-615px', 'left': '50%'});
}
});
我知道这是一个有点混乱,黑客攻击有点和,但我还是想获得它的功能。
我添加了两个班,并给DIV comBrand类的最大化,在html硬编码,然后在调整大小功能,使用此代码,但是,它不能正常工作或做任何事......
$(window).resize(function() {
if ($('#comBrand').hasClass('maximized'))
{
$('#comBrand').removeClass('maximized');
$('#comBrand').addClass('minimized');
}
else {
$('#comBrand').removeClass('minimized');
$('#comBrand').addClass('maximized');
}
});
'在resize事件我使用.css()函数将不同的属性应用于该元素,看到该部分会很有趣。你有没有考虑过使用媒体查询?为什么你检查'if($('#comBrand').css('top')=='155px')'而不是特定的窗口宽度?你也使用这个逻辑:'if === 155' else else'$('#comBrand')。css({'top':'155px','对我来说没有意义......很难理解你实际上需要 – 2014-10-30 19:04:05