admin管理员组文章数量:1278910
I'm using .toggle()
on a button
element:
$("header button").click(function(event){
$(".site-nav-wrapper").toggle();
event.preventDefault();
});
This works great. The problem is if the button
is toggled to display:none
and then I change the device orientation, triggering my desktop media query, despite the fact I re-force display:block;
on the desktop media query, the button
remains toggled to display:none
:
(Sass):
.site-nav-wrapper{
//Mobile First
display:none;
@include breakpoint($breakpoint-lg) {
display:block;
}
}
Is there a way to reset whatever the toggle()
function is storing?
I'm using .toggle()
on a button
element:
$("header button").click(function(event){
$(".site-nav-wrapper").toggle();
event.preventDefault();
});
This works great. The problem is if the button
is toggled to display:none
and then I change the device orientation, triggering my desktop media query, despite the fact I re-force display:block;
on the desktop media query, the button
remains toggled to display:none
:
(Sass):
.site-nav-wrapper{
//Mobile First
display:none;
@include breakpoint($breakpoint-lg) {
display:block;
}
}
Is there a way to reset whatever the toggle()
function is storing?
-
You can try removing the element's
style
attribute:$("header button").removeAttr('style');
– Blender Commented Aug 21, 2012 at 2:58
3 Answers
Reset to default 8toggle
uses inline styling, which overrides whatever you're doing in your stylesheet.
To get the desired result, you should use a special hidden
class to hide the element, and use toggleClass
instead.
SASS:
.site-nav-wrapper.hidden {
display: none;
@include breakpoint($breakpoint-lg) {
display: block;
}
}
JS:
$("header button").click(function(event){
$(".site-nav-wrapper").toggleClass('hidden');
event.preventDefault();
});
If I remember correctly toggle
sets the display
property to none
on the element's style
attribute. If you want to force the button to display in the "desktop" orientation then you'll need to include an !important
declaration in your desktop breakpoint.
Alternately, you can listen for onorientationchanged
and switch to the desktop version of the site then (un-toggling / activating everything that needs to be activated).
Why not avoid the toggle so there's no issues with the media queries.
$("header button").click(function(event){
$(".site-nav-wrapper:visible").hide();
$(".site-nav-wrapper:hidden").show();
event.preventDefault();
});
本文标签: javascriptjQuery toggle() and Media QueriesStack Overflow
版权声明:本文标题:javascript - jQuery .toggle() and Media Queries - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741303137a2371213.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论