简体   繁体   English

在CSS更改时使用Jquery触发事件?

[英]Trigger event using Jquery on CSS change?

I'm curious is there an event listener or perhaps a way to construct a method that will trigger when a CSS change happens? 我很好奇是否有一个事件监听器或者一种方法来构造一个在CSS发生变化时会触发的方法?

My stylesheet uses media queries and I want to know if there's a way to attach a listener to see when those media queries kick in and out. 我的样式表使用媒体查询,我想知道是否有一种方法可以附加一个监听器,以查看这些媒体查询何时启动和退出。 For example I have a media query that hides a button at certain screen widths 例如,我有一个媒体查询,隐藏某个屏幕宽度的按钮

@media screen and (max-width: 480px) {
  #search-button {
    display: none;
  }
}

What event listener would I use to detect when that display changes? 我将使用什么事件监听器来检测该显示何时发生变化? I'm currently doing this: 我现在正在这样做:

$(window).resize(function() {
  if($('#search-button').css("display") == "none") {
    //do something
  } else {
    //do something else
  }
});

Which works fine, but it calls the listener every time the user changes the screen and I'd rather just have it fire only when the css of the button changes. 哪个工作正常,但每次用户更改屏幕时都会调用监听器,而我只是在按钮的css发生变化时才激活它。 I hope that makes sense. 我希望这是有道理的。

for example this is what I'd like 例如,这就是我想要的

$('#search-button').cssEventListenerOfSomeKind(function() {
  alert('the display changed');
});

Binding to the window.resize is your best option (I believe). 绑定到window.resize是你最好的选择(我相信)。 There isn't any event fired when you change an element's CSS. 更改元素的CSS时没有触发任何事件。 You can however optimize a bit by caching the selector used: 但是,您可以通过缓存使用的选择器来优化一点:

var $searcButton = $('#search-button');
$(window).resize(function() {
    if($searcButton.css("display") == "none") {
        //do something
    } else {
        //do something else
    }
});

Or you can use $(window).width() to check the width of the viewport: 或者您可以使用$(window).width()来检查视口的宽度:

var $window = $(window);
$window.resize(function() {
    if($window.width() <= 480) {
        //do something
    } else {
        //do something else
    }
});

UPDATE UPDATE

You can always throttle your own event handler: 您始终可以限制自己的事件处理程序:

var $window   = $(window),
    resize_ok = true,
    timer;

timer = setInterval(function () {
    resize_ok = true;
}, 250);

$window.resize(function() {
    if (resize_ok === true) {
        resize_ok = false;
        if($window.width() <= 480) {
            //do something
        } else {
            //do something else
        }
    }
});

This will prevent the code in your resize event handler from running more than once every quarter second. 这将阻止resize事件处理程序中的代码每四分之一运行一次以上。

I know this is old but I managed to solve it with this logic 我知道这是旧的,但我设法用这个逻辑来解决它

    // set width and height of element that is controlled by the media query
    var page_width = $page.width();
    var page_height = $page.height();


    $window = $(window).resize(function(){
        if( $page.width() != page_width ) {
            // update page_width and height so it only executes your 
            // function when a change occurs
            page_width = $page.width();
            page_height = $page.height();
            // do something
            // ...
        }
    });

If it is only a one time event you could try to unbind the event. 如果它只是一次性事件,您可以尝试解除绑定事件。

http://api.jquery.com/unbind/ http://api.jquery.com/unbind/

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

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