tags:

views:

298

answers:

5

How come this doesn't create a vertical 5px-wide bar on the ride side of the window?

<html>
<head runat="server">
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">

        $(document).ready(function() {
            $("#scrollBar").css({
                height: $(window).height,
                width: 5,
                position: "absolute",                
                top: 0,
                left: $(window).width - 5,
                margin: 0
            });
        });

    </script>
</head>
<body>
    <div id="scrollBar" style="background-color: Red">test
    </div>
</body>
</html>
A: 

Instead of left: $(window).width - 5, you could try right: 0.

EDIT: Yeah, that was pretty stupid. But, if I'm not mistaken, those should also be quoted, as in 'width': '5px',.

  $("#scrollBar").css({
    'height': $(window).height(),
    'width': '5px',
    'position': 'absolute',
    'top': 0,
    'right': 0,
    'margin': 0
  });
theIV
+1  A: 

Also note that jQuery's .width and .height should be .width() and .height().

Examples of use...

Jonathan Sampson
+1  A: 

you might want to put "5px" instead of 5 for the width and $(window).height() + "px" for the height and ($(window).width() - 5) + "px" for the left.

John Boker
+6  A: 

Use this instead:

I found several problems:

  • instead of $(window).height, use $(window).height()
  • instead of left: $(window).width - 5, use right: 0

    $(document).ready(function() {
        $("#scrollBar").css({
            height: $(window).height(),
            width: 5,
            position: "absolute",                
            top: 0,
            right: 0,
            margin: 0
        });
    });
    
Randell
The only problem with 'right' is that IE often mucks it up and is 1px off.
Pat
Would padding/margin help? I don't have IE right now so I can't test it.
Randell
tested on ie8.works.
krishna
A: 

From reading jQuery documentation you can see that you need to put the css properties and their values as string values.

 $("#scrollBar").css({
                'height': $(window).height(),
                'width': '5px',
                'position': 'absolute',                
                'top': '0',
                'left': $(window).width() - 5,
                'margin': '0'
            });

http://docs.jquery.com/CSS/css#properties

I'm not really sure tough how you would display those calculated values.

PS: Jonathas was right height and width are functions, not properties.

Guilherme