2014-11-21 51 views
0

我想了解如何將窗口大小事件內的一個值傳遞給函數,但我不知道我在做什麼錯誤。基本上每次窗口調整大小時,我都希望scrollHeight能夠轉到另一個功能,我可以在點擊時查看該值。如何將窗口調整大小的值傳遞給另一個函數

$(window).on('resize', function(){ 
var mheight = $('#wrapper')[0].scrollHeight; 
smoothscroll(mheight); 
}); 

function smoothscroll(mvalue) { 
$mheight = this.mvalue; 

if ($mheight < 3000) { 
$(selector).on('click',function() { 
console.log('show me my latest scroll height value for larger screens' + $mheight); 
}); 
} else { 
console.log('show me my current scroll height value for smaller screens' + $mheight); 
} 

} 

...但由於某些原因我始終值出現未定義...

回答

1

變化

function smoothscroll(mvalue) { 
    $mheight = this.mvalue; 
    .... 
} 

function smoothscroll(mvalue) { 
    $mheight = mvalue; // you don't need `this`, cause mvalue has passed by argument 
    .... 
} 

其實你可以直接使用mvalue沒有緩存到$mheight

完整代碼:

$(window).on('resize', function() { 
    var mheight = $('#wrapper')[0].scrollHeight; 
    smoothscroll(mheight); 
}); 

function smoothscroll(mvalue) { 
    if (mvalue < 3000) { 
     $(selector).on('click', function() { 
      console.log('show me my latest scroll height value for larger screens' + mvalue); 
     }); 
    } else { 
     console.log('show me my current scroll height value for smaller screens' + mvalue); 
    } 

} 
相關問題