1

I am using this code to trigger on click button .How can I change this when page load?

$(document).ready(function() {
    $('#slideleft button').click(function() {
    var $lefty = $(this).next();
    $lefty.animate({left: parseInt($lefty.css('left'),10) == 0 ? -$lefty.outerWidth() :      0});
  });
}

My html

<div id="slideleft" class="slide">
<button>slide it</button>
<div class="inner" style="left: -350px;">Animate this element's left style property</div>
</div>

I want to slide in when the page load not clicking the button.

1
  • 1
    Put the animate call directly in the .ready() function, not in the click function. Commented Feb 6, 2014 at 18:25

2 Answers 2

1

Run the code when document is ready or on widow load

$(document).ready(function() {
    var $lefty = $('#slideleft button').next();
    $lefty.animate({left: parseInt($lefty.css('left'),10) == 0 ? -$lefty.outerWidth() :      0});
}

on window load

$(window).load(function() {
    var $lefty = $('#slideleft button').next();
    $lefty.animate({left: parseInt($lefty.css('left'),10) == 0 ? -$lefty.outerWidth() :      0});
});

for speed

$lefty.animate({left: parseInt($lefty.css('left'),10) == 0 ? -$lefty.outerWidth() : 0}, 1000);

the 1000 is 1000 milliseconds, you can also add callback on animation complete, for more details go to http://api.jquery.com/animate/

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

1 Comment

Thank you .Is there any way to control slide speed?
0

If you want to completely loading the page then the effect start you can do this :

$(window).load(function () {

     var $lefty = $('#slideleft button').next();
     $lefty.animate({left: parseInt($lefty.css('left'),10) == 0 ? -$lefty.outerWidth() :      0});

});

This effect starts after finishing page loading. But if you want to start the effect real time you can use this :

$(document).ready(function () {

     var $lefty = $('#slideleft button').next();
     $lefty.animate({left: parseInt($lefty.css('left'),10) == 0 ? -$lefty.outerWidth() :      0});

});

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.