Jquery.height() returns different results using F5 or CTRL+F5

后端 未结 5 2148
心在旅途
心在旅途 2021-01-22 10:40

So I am trying to find the height of my images then add a top margin this enables me to impose a a vertical center.

I\'m running this code, and on an F5 refresh<

5条回答
  •  心在旅途
    2021-01-22 10:53

    Try this approach:

    jQuery(function() {
        jQuery('.imagedisplay img').each(function() {
            var $this   = jQuery(this),
                height  = $this.height();
            if (height) {
                $this.css('margin-top', ((240 - height) / 2) + 'px');
            } else {
                $this.on('load', function() {
                    $this.css('margin-top', ((240 - $this.height()) / 2) + 'px');
                });
            }
        });
    });
    

提交回复
热议问题