AngularJS event on window innerWidth size change

前端 未结 4 1411
再見小時候
再見小時候 2020-11-30 20:27

I\'m looking for a way to watch changes on window inner width size change. I tried the following and it didn\'t work:

$scope.$watch(\'window.innerWidth\', fu         


        
4条回答
  •  旧时难觅i
    2020-11-30 21:19

    If Khanh TO's solution caused UI issues for you (like it did for me) try using $timeout to not update the attribute until it has been unchanged for 500ms.

    var oldWidth = window.innerWidth;
    $(window).on('resize.doResize', function () {
        var newWidth = window.innerWidth,
            updateStuffTimer;
    
        if (newWidth !== oldWidth) {
            $timeout.cancel(updateStuffTimer);
        }
    
        updateStuffTimer = $timeout(function() {
             updateStuff(newWidth); // Update the attribute based on window.innerWidth
        }, 500);
    });
    
    $scope.$on('$destroy',function (){
        $(window).off('resize.doResize'); // remove the handler added earlier
    });
    

    Reference: https://gist.github.com/tommaitland/7579618

提交回复
热议问题