0

I have some attributes that I am trying to change with the .css in jquery but for some reason the code does not work. Is there something wrong with the parenthesis?

the original value is set as so padding: 40px 0 0;

here is the jquery that changes the above value:

var header = $('#main'), padding = '40px'; $(window).scroll(function() { if ($(this).scrollTop() <= padding / 2) { header.css({ 'padding-top': -($(this).scrollTop() - padding) + "px", 'padding-bottom': -($(this).scrollTop() - padding) + "px" }); 
19
  • What error are you receiving in your browser's console? Commented Jul 1, 2013 at 20:11
  • @kyle.stearns not receiving one the console is blank Commented Jul 1, 2013 at 20:12
  • You need to show more code Commented Jul 1, 2013 at 20:12
  • I don't know about the parentheses, but you can't have negative values in the padding; they're invalid lengths and will be discarded by the browser. Commented Jul 1, 2013 at 20:13
  • 1
    $(this).scrollTop() <= padding / 2 is always false: '40px' / 2 is NaN, and any compaison involving NaN is false. Commented Jul 1, 2013 at 20:18

1 Answer 1

2

The following line does not result in something sensible

if ($(this).scrollTop() <= padding / 2) { 

padding is a string. I have no clue what "40px" / 2 would be, but I doubt javascript will make it 20px. Make padding an integer, and change it to (padding / 2) + "px".

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.