0

我有一个 jQuery 函数,如果满足某些条件,它会更改列的大小。调整窗口大小后,该功能开始工作。但是一旦条件返回,它就不会停止重新计算false。怎么可能解决?这是一个脚本:

jQuery(document).ready(function ($) {

    element_second = document.getElementsByClassName('second_column')[0];
    element = document.getElementsByClassName('first_column_main_page')[0];


    $(window).on('resize', function(){      
        interval = setInterval(updateHeight,3000);
    });


  function updateHeight(){    

    var $window = $(window);
    var width = $window.width();
    var height = $window.height();
        if (height > 667 && height < 800) {
            interval = setInterval(updateHeight,300);
            console.log('true');
            setInterval(function () {
                if ((width != $window.width()) || (height != $window.height())) {
                    width = $window.width();
                    height = $window.height();

                    element.style.height = ((height - element.offsetTop) + "px");
                    element_second.style.height = ((height - element.offsetTop) + "px");    
                }
            }, 300);
        } 
        else {
            console.log('false');
            clearInterval(interval);        
            element.style.height = (840 + "px");//
            element_second.style.height = (840 + "px");// this part makes all blocks to flash, cause recalculations dosn's stop
        }            
  };            
});

换句话说,我需要一个脚本来重新计算特定尺寸的高度。

@Titus 回答

 jQuery(document).ready(function ($) {


    element_second = document.getElementsByClassName('second_column')[0];
    element = document.getElementsByClassName('first_column_main_page')[0];


    $(window).on('resize', function(){      
        updateHeight();
    });


  function updateHeight(){    

        var $window = $(window);
        var width = $window.width();
        var height = $window.height();

        if (height > 667 && height < 800) {

            console.log('true');

{
                width = $window.width();
                height = $window.height();

                element.style.height = ((height - element.offsetTop) + "px");
                element_second.style.height = ((height - element.offsetTop) + "px");    


        } 
        else {
            console.log('false');                   
        }            
  };    

});
4

1 回答 1

0

@Titus 回答

 jQuery(document).ready(function ($) {


    element_second = document.getElementsByClassName('second_column')[0];
    element = document.getElementsByClassName('first_column_main_page')[0];


    $(window).on('resize', function(){      
        updateHeight();
    });


  function updateHeight(){    

        var $window = $(window);
        var width = $window.width();
        var height = $window.height();

        if (height > 667 && height < 800) {

            console.log('true');

{
                width = $window.width();
                height = $window.height();

                element.style.height = ((height - element.offsetTop) + "px");
                element_second.style.height = ((height - element.offsetTop) + "px");    


        } 
        else {
            console.log('false');                   
        }            
  };    

});
于 2019-07-13T13:16:15.633 回答