到达 CSS 个断点时触发事件
Trigger events when CSS breakpoints are reached
我有一组断点,我想在每次通过时触发一个事件。目前,我正在使用 $(document).resize(function(){})
,但这与我的 CSS 断点不匹配,无论我使用 window
、document
还是任何其他选择器。
有什么方法可以检测媒体查询何时通过?这是我当前的代码:
$( window ).resize(
function() {
if( $(window).width() < 500 ) {
$(window).trigger("breakpoint-sm");
}
if( $(window).width() < 900 ) {
$(window).trigger("breakpoint-md");
}
}
);
$(window).on(
"breakpoint-md", function() {
if($(window).width() < 900) {
// this happens when below medium screen size
alert( "breakpoint reached" );
}
}
);
@media screen and (max-width: 500px) {
/* do mobile things */
}
@media screen and (max-width: 900px) {
/* do mobile things */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
如果有更简单的方法知道断点是向上还是向下传递,我很乐意听到。
谢谢!
我有一个解决你的确切问题的方法,我自己使用。
基本上,您不能使用JavaScript 直接检测断点,但您可以检测由断点引起的元素变化。当达到各自的断点时,.css-js_ref-*
div 将变得可见。
<div class="css-js_ref">
<div class="css-js_ref-sm" data-bp="sm"></div>
<div class="css-js_ref-md" data-bp="md"></div>
</div>
然后您可以使用 JS 检测最后一个可见元素是什么:
function currentBreakpoint() { return $('.css-js_ref > *:visible').first().attr('data-bp') };
此 returns 您放入 .css-js_ref
标记中的断点名称,即 sm
.
工作示例:
function currentBreakpoint() { return $('.css-js_ref > *:visible').first().attr('data-bp') };
var breakpointLength = $('.css-js_ref > *:visible').length;
$(window).on('resize', function () {
var newBreakpointLength = $('.css-js_ref > *:visible').length;
if (newBreakpointLength < breakpointLength) {
breakpointLength = newBreakpointLength;
$(window).trigger('breakpoint:up', [currentBreakpoint()]);
}
if (newBreakpointLength > breakpointLength) {
breakpointLength = newBreakpointLength;
$(window).trigger('breakpoint:down', [currentBreakpoint()]);
}
});
$(window).on('breakpoint:down', function(event, bp){
console.log(bp);
});
.css-js_ref * {
display: none;
}
@media screen and (max-width: 500px) {
.css-js_ref-sm {
display: block;
max-width: 500px;
}
}
@media screen and (max-width: 900px) {
.css-js_ref-md {
display: block;
max-width: 900px;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="css-js_ref">
<div class="css-js_ref-sm" data-bp="sm"></div>
<div class="css-js_ref-md" data-bp="md"></div>
</div>
用法:
// bp is the breakpoint that was reached
$(window).on('breakpoint:down', function(event, bp){
if(bp === 'md') {
// do stuff on below medium sized devices
}
});
$(window).on('breakpoint:up', function(event, bp){
if(bp === 'md') {
// do stuff on above medium sized devices
}
});
这个解决方案有点麻烦,但非常通用。这也意味着您只需在一个地方定义断点,这对 DRY 合规性非常有用。
我有一组断点,我想在每次通过时触发一个事件。目前,我正在使用 $(document).resize(function(){})
,但这与我的 CSS 断点不匹配,无论我使用 window
、document
还是任何其他选择器。
有什么方法可以检测媒体查询何时通过?这是我当前的代码:
$( window ).resize(
function() {
if( $(window).width() < 500 ) {
$(window).trigger("breakpoint-sm");
}
if( $(window).width() < 900 ) {
$(window).trigger("breakpoint-md");
}
}
);
$(window).on(
"breakpoint-md", function() {
if($(window).width() < 900) {
// this happens when below medium screen size
alert( "breakpoint reached" );
}
}
);
@media screen and (max-width: 500px) {
/* do mobile things */
}
@media screen and (max-width: 900px) {
/* do mobile things */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
如果有更简单的方法知道断点是向上还是向下传递,我很乐意听到。
谢谢!
我有一个解决你的确切问题的方法,我自己使用。
基本上,您不能使用JavaScript 直接检测断点,但您可以检测由断点引起的元素变化。当达到各自的断点时,.css-js_ref-*
div 将变得可见。
<div class="css-js_ref">
<div class="css-js_ref-sm" data-bp="sm"></div>
<div class="css-js_ref-md" data-bp="md"></div>
</div>
然后您可以使用 JS 检测最后一个可见元素是什么:
function currentBreakpoint() { return $('.css-js_ref > *:visible').first().attr('data-bp') };
此 returns 您放入 .css-js_ref
标记中的断点名称,即 sm
.
工作示例:
function currentBreakpoint() { return $('.css-js_ref > *:visible').first().attr('data-bp') };
var breakpointLength = $('.css-js_ref > *:visible').length;
$(window).on('resize', function () {
var newBreakpointLength = $('.css-js_ref > *:visible').length;
if (newBreakpointLength < breakpointLength) {
breakpointLength = newBreakpointLength;
$(window).trigger('breakpoint:up', [currentBreakpoint()]);
}
if (newBreakpointLength > breakpointLength) {
breakpointLength = newBreakpointLength;
$(window).trigger('breakpoint:down', [currentBreakpoint()]);
}
});
$(window).on('breakpoint:down', function(event, bp){
console.log(bp);
});
.css-js_ref * {
display: none;
}
@media screen and (max-width: 500px) {
.css-js_ref-sm {
display: block;
max-width: 500px;
}
}
@media screen and (max-width: 900px) {
.css-js_ref-md {
display: block;
max-width: 900px;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="css-js_ref">
<div class="css-js_ref-sm" data-bp="sm"></div>
<div class="css-js_ref-md" data-bp="md"></div>
</div>
用法:
// bp is the breakpoint that was reached
$(window).on('breakpoint:down', function(event, bp){
if(bp === 'md') {
// do stuff on below medium sized devices
}
});
$(window).on('breakpoint:up', function(event, bp){
if(bp === 'md') {
// do stuff on above medium sized devices
}
});
这个解决方案有点麻烦,但非常通用。这也意味着您只需在一个地方定义断点,这对 DRY 合规性非常有用。