Bind to ready and resize at same time using jQuery .on()

后端 未结 3 1073
粉色の甜心
粉色の甜心 2021-01-05 08:02

This works for running the same code on both ready and resize:

$(document).ready(function() {

    $(window).resize(function() {

         // Stuff in here h         


        
3条回答
  •  滥情空心
    2021-01-05 08:20

    .ready(), .resize(), and others like .mouseover() are all just short-cuts for using the .bind() function (or .on() in jQuery 1.7+). .resize(function () {}) maps to .bind('resize', function () {}). Here is how your code would look using .on() wherever possible:

    $(document).on('ready', function() {
    
        $(window).on('resize', function() {
    
             // Stuff in here happens on ready and resize.
    
        }).trigger('resize'); // Trigger resize handlers.       
    
    });//ready
    

    Here is a demo: http://jsfiddle.net/qMBtP/

提交回复
热议问题