2

I am creating a jQuery function which, on window resize, performs the following task:

If window width increases, call profdisplayer function. If window width decreases call profhider function.

I don't know what is wrong with this and how to resolve this.

 $(window).on('resize', function(event){
    var awidth = $(window).width();
    var bwidth=0;
    if (awidth>bwidth)
        {   profdisplayer();
            bwidth = awidth;
        }
    if (bwidth>awidth)
    {
        profhider();
        bwidth=awidth;
        }
    });
4

3 回答 3

1

You have to initialize bwidth the first time the page loads, in the document ready event. And don't forget to remove the bwidth declaration from the function.

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

于 2013-10-20T13:46:20.223 回答
1

而不是在 resize 事件中声明 bwidth 在外部/全局声明它

var bwidth = 0;
$(window).on('resize', function(event){
    var awidth = $(window).width();
    if (awidth>bwidth)
        {   profdisplayer();
            bwidth = awidth;
        }
    if (bwidth>awidth)
    {
        profhider();
        bwidth=awidth;
        }
    });
于 2013-10-20T14:10:32.430 回答
0
if (awidth>bwidth) // all time false

if (awidth>bwidth) // all time false

Your function does nothing.

于 2013-10-20T13:46:59.337 回答