0

I am trying to add some style elements to my jquery code.

My objective is to be able to position a box absolutely after the person has scrolled down the screen after 200px.

The box reduces in size after a 200px scroll but i want to have the ability to position the box where i want.

Here is my code.

Can some one guide me on where I am going wrong.

Fiddle

  $( window ).scroll(function() {
     if($(window).scrollTop() > 200){
       $('#profile-pic-bg').css({'width': '50'});
        $("#profile-pic-bg").css({ position: "absolute", top: "20", left:"5" });
     }else{
         $('#profile-pic-bg').css({'width': '145'});
     }

});
11
  • 3
    how is java related here? please remove java tag Commented Nov 22, 2013 at 11:48
  • 1
    I didn't understand the point here, your code works fine unless you mean that you want a "floating box" PS: Remove Java Tag. Commented Nov 22, 2013 at 11:49
  • 2
    @user2965875, Jquery Is a Javascript library, Java is a thing, and Javascript is something else. Commented Nov 22, 2013 at 11:50
  • 2
    Somewhat off topic but Java and Javascript are similar like Car and Carpet are similar Commented Nov 22, 2013 at 11:52
  • 5
    What a useless discussion, just change the tag and be done with it. Commented Nov 22, 2013 at 11:53

2 Answers 2

4

Remove the quotes from the top and left property values, or they wont work:

$("#profile-pic-bg").css({ position: "absolute", top: 20, left: 5 });
Sign up to request clarification or add additional context in comments.

4 Comments

top: "20", left:"5" -> top: 20, left: 5
Yeah I seen... thought you meant the actual properties, but you meant the property values
@David also if he wants it with quote then he should use px like "20px"
@C-Link true, but unless you are using other units than pixels it doesn’t make a difference. I prefer integers, some prefer strings...
1

Try with this

$( window ).scroll(function() {
     if($(window).scrollTop() > 200){

        $("#profile-pic-bg").css({ 
            "position": "absolute", 
            "top": "20px", 
            "left":"5px" ,
            "width":'50px'
        });
     }else{
         $('#profile-pic-bg').css({'width': '145px'});
     }

});

DEMO

5 Comments

Thanks for this ! great help. Can you show me how you would add style to the box width 145px too?
if scroll return again 200 below the height is set it to the selector see fiddle
if you check the fiddle the and move back up the screen the box does not pick up the original style. it moves out of place.
"background-image": "url('../img/assets/profile-pic-bg.png') 0px 0px no-repeat !important;"
I have tried to add a background image into the jquery but its not working? have i done somthing wrong?

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.