0

I have made a very basic social slider bar that hides at the side of my site, then slides out on hover. It all works if i give the jQuery set values for the current css 'left' property, however when i try and get the left property and set it as a variable the script stops working. Have i missed something?

$('#slider').mouseenter(function () {

    var cssleft = $(this).css('left');

    if ($('#slider').css('left') === "cssleft") {
        $(this).animate({
            left: '0'
        }, 300, function () {});
    } else {
        $(this).animate({
            left: ("cssleft" + 'px')
        }, 100, function () {});
    }

});

$('#slider').mouseleave(function () {
    $(this).delay(1000).animate({
        left: ("cssleft" +  'px')
    }, 500, function () {});
});

There is a working version with set values here

http://jsfiddle.net/TjTNm/

Thanks guys!

0

3 Answers 3

2

You could also do this entirely in CSS3, without any jQuery: See this update of your fiddle.

#slider{
    position:fixed;
    left:-70px;
    padding:10px;
    border:1px solid #f00;
    transition:left 0.5s ease 1s;
    -webkit-transition:left 0.5s ease 1s;
}

#slider:hover {
    left:0;
    transition:left 0.5s;
    -webkit-transition:left 0.5s;
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your response. I would have preferred to do it this way but it has to be compatible with IE7 unfortunately.
1

Define and initialize your left offset variable outside of your jQuery actions:

var cssleft = $('#slider').css('left');

$('#slider').mouseenter(function () {
    if ($('#slider').css('left') === cssleft) {
        $(this).animate({
            left: '0'
        }, 300, function () {});
    } else {
        $(this).animate({
            left: cssleft
        }, 100, function () {});
    }

});

$('#slider').mouseleave(function () {
    $(this).delay(1000).animate({
        left: cssleft
    }, 500, function () {});
});

Also, do not put quotes around the variable cssleft otherwise it will be interpreted as a string.

See: http://jsfiddle.net/audetwebdesign/qR7z5/

Footnote

The HTML5 approach using transitions is much more elegant, as shown in the post by theftprevention

Comments

0

You will need to remove the inverted commas from around the cssleft, it's also not defined on the mouseleave, the cssleft variable is local to your mousenter function.:

$('#slider').mouseenter(function () {

    var cssleft = $(this).css('left');

    if ($('#slider').css('left') === "cssleft") {
        $(this).animate({
            left: '0'
        }, 300, function () {});
    } else {
        $(this).animate({
            left: (cssleft + 'px')
        }, 100, function () {});
    }

});

$('#slider').mouseleave(function () {
    $(this).delay(1000).animate({
        left: (cssleft +  'px')
    }, 500, function () {});
});

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.