简体   繁体   中英

jQuery to vertically center text only loads after page resize

I have the following jQuery to vertically center text next to an image but it only loads after the page is re-sized.

$(window).load(function () {
    $(function () {
        var changeheight = function () {
            $('.vertical-align').height($('.featurette-image').height());
        }
        $(window).on('resize', function () {
            if ($(window).width() > 765) {
                changeheight();
            } else {
                $('.vertical-align').height('auto');
            }
        })
    })
});   

Remove window resize and should work on load

 $(window).load(function() { var changeheight = function() { $('.vertical-align').height($('.featurette-image').height()); } if ($(window).width() > 765) { changeheight(); } else { $('.vertical-align').height('auto'); } }); 

Idk why OP deleted his answer but it was correct. I had to remove window resize

The following code is correct:

$(window).load(function() {
  var changeheight = function() {
    $('.vertical-align').height($('.featurette-image').height());
  }
  if ($(window).width() > 765) {
    changeheight();
  } else {
    $('.vertical-align').height('auto');
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM