javascript - JQuery recalculate css on window resize - Stack Overflow

On this site:Although the slider is full widthvariable on browser window size, the Title of each s

On this site: /

Although the slider is full width / variable on browser window size, the Title of each slider item is always indented to line up with the content.

I've tried setting this up: /

$(window).resize(function() {
    $('#item').css("padding-left",$(window).width()/2);
});

The function is working but the calculation pushes the item too far in on decreasing window size and too far out on increase.

The line in the working slider example is:

$('.layout-feature .box-section-image-gallery-controls-title, .layout-feature .box-section-image-gallery-title').css('paddingLeft', 60 + extraSpace/2);

where extraSpace is the $(window).width()

Any help gratefully received - thanks in advance

On this site: http://houston.aiga/

Although the slider is full width / variable on browser window size, the Title of each slider item is always indented to line up with the content.

I've tried setting this up: http://jsfiddle/topiman/xS7vn/

$(window).resize(function() {
    $('#item').css("padding-left",$(window).width()/2);
});

The function is working but the calculation pushes the item too far in on decreasing window size and too far out on increase.

The line in the working slider example is:

$('.layout-feature .box-section-image-gallery-controls-title, .layout-feature .box-section-image-gallery-title').css('paddingLeft', 60 + extraSpace/2);

where extraSpace is the $(window).width()

Any help gratefully received - thanks in advance

Share Improve this question edited Apr 5, 2013 at 15:15 pb2q 59.7k19 gold badges150 silver badges152 bronze badges asked Apr 5, 2013 at 15:02 topimantopiman 5112 gold badges6 silver badges15 bronze badges 2
  • 1 shouldn't you take notice of the width of the body as well, not just the window width? – Luceos Commented Apr 5, 2013 at 15:11
  • Updated fiddle: jsfiddle/topiman/xS7vn/5 Tried using: window, 'body', document - they all 'fire' correctly but I can't get the calculations to be the same as on the example site: houston.aiga – topiman Commented Apr 5, 2013 at 16:53
Add a ment  | 

1 Answer 1

Reset to default 2

It seems you forgot about the width after all: http://jsfiddle/topiman/xS7vn/5/

This is what I came up with. Stupid thing is the console.log kept giving back a +8 difference which I had to hardcode remove. Is this what you were looking for?

$(window).resize(function() {
    var ItemWidth = $(".wrapper").width();
    $('#item').width( ItemWidth );
    var WindowWidth = $(window).width();
    // cannot resize because window is smaller than the wrapper
    if( ItemWidth > WindowWidth ) {
        var PadWidth = 0;
    } else {
        var PadWidth = (WindowWidth - ItemWidth)/2 - 8;
    }
    console.log( ItemWidth + " - " + WindowWidth + " - " + PadWidth );

    $('#item').css("margin-left",PadWidth + "px");
    });

Update; also check http://jsfiddle/xS7vn/8/ for the latest update including resizing on page load.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745316440a4622239.html

相关推荐

  • javascript - JQuery recalculate css on window resize - Stack Overflow

    On this site:Although the slider is full widthvariable on browser window size, the Title of each s

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信