文档准备好并调整文档大小时调用事件

时间:2016-10-05 21:12:05

标签: jquery

我使用以下脚本来获取div的填充顶部。 div从英雄形象的高度得到了他的填充顶部。加载大小时,actuall脚本工作正常,但是当我调整浏览器窗口大小时,不会再次计算填充顶部。

jQuery(document).ready(function($) {
    /***** CONTENT-WRAPPER PADDING FRONTPAGE *****/
    var $banner = $(".front header img");
    $banner.on('load', function(){
        var bannerHeight = $(this).height();
        console.log(bannerHeight);
        $("#content-wrapper").css("padding-top", bannerHeight)
    });
})

2 个答案:

答案 0 :(得分:1)

试试这个:

jQuery( window ).resize(function() {
  var bannerHeight = $(".front header img").height();
  console.log(bannerHeight);
  $("#content-wrapper").css("padding-top", bannerHeight)
});

答案 1 :(得分:0)

每次调整窗口大小时都会触发resize()事件。将处理程序附加到两个事件:#import <XCTest/XCTest.h> @interface TestNSOperationKVO : XCTestCase @end @implementation TestNSOperationKVO { NSMutableArray<NSDictionary *> *changes; } NSString *kKeyPath = @"keyPath"; NSString *kChange = @"change"; - (void)setUp { [super setUp]; changes = [NSMutableArray array]; } - (void)tearDown { [super tearDown]; } - (void)testIsExecutingKVO { NSOperation *op = [NSBlockOperation blockOperationWithBlock:^{}]; [op addObserver:self forKeyPath:NSStringFromSelector(@selector(isExecuting)) options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:NULL]; [op addObserver:self forKeyPath:NSStringFromSelector(@selector(isFinished)) options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:NULL]; [op start]; XCTAssertEqual(changes.count, 3); XCTAssert([@"isExecuting" isEqualToString:changes[0][kKeyPath]]); XCTAssertEqual(changes[0][kChange][NSKeyValueChangeOldKey], @NO); XCTAssertEqual(changes[0][kChange][NSKeyValueChangeNewKey], @YES); XCTAssert([@"isExecuting" isEqualToString:changes[1][kKeyPath]]); // this fails XCTAssertEqual(changes[1][kChange][NSKeyValueChangeOldKey], @YES); // this fails XCTAssertEqual(changes[1][kChange][NSKeyValueChangeNewKey], @NO); XCTAssert([@"isFinished" isEqualToString:changes[2][kKeyPath]]); XCTAssertEqual(changes[2][kChange][NSKeyValueChangeOldKey], @NO); XCTAssertEqual(changes[2][kChange][NSKeyValueChangeNewKey], @YES); } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context { [changes addObject:@{kKeyPath: keyPath, kChange: change}]; } @end onReady

resize