0
$(document).on(".wrapper-parallax", function(){
    var height = $(window).width() / 8; 

    $(this).css("margin-top",height);
}

this code seem doesn't run in 'real time', I want the result (margin-top's value) to change along I resizing the window.

1
  • I think you are trying var height = $(window).height() / 8; instead. Commented Dec 17, 2013 at 9:04

3 Answers 3

3

You need to write a window resize handler

$(window).resize(function () {
    var height = $(window).width() / 8; //may be height() here instead of width()
    $(".wrapper-parallax").css("margin-top", height);
}).resize()
Sign up to request clarification or add additional context in comments.

2 Comments

I've never seen like that: $(window).resize(....).resize() can you explain about it?
@C-link that is to call the resize handler on initialization so that when the page loads you will have the correct value set..
0

You are missing trailing px

Try:

$(document).on(".wrapper-parallax", function(){
    var height = $(window).width() / 8; 
    $(this).css("margin-top",height+"px");
}

Comments

0

Try this:

$(window).resize(function(){
    var height = $(this).height() / 8; 

    $('.wrapper-parallax').css("margin-top", height + 'px');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.