When the page is loaded, the scroll bar automatically scrolls to a certain position _javascript tips

Source: Internet
Author: User
Yesterday there is a need, is to want to load the page after the left automatically scroll a certain position.

Always thought that as long as the page document.documentElement.scrollLeft set a numerical value to be effective, the result is disappointed ~
Today, I found out:
The use of Document.documentElement.scrollLeft set values, must be triggered by human events to take effect;
To automatically scroll a certain distance when the page is loaded, use the animate of jquery, as in the following example:

$ ("Html,body"). Animate ({"ScrollLeft": "300px"}, 1000);
$ ("Html,body"). Animate ({"scrolltop": "300px"}, 1000);

Demo
Copy Code code as follows:

<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title> Automatic scrolling </title>
<meta name= "keywords" content= ""/>
<meta name= "description" content= ""/>
<script type= "Text/javascript" src= "Https://www.gamebox.com/js/jquery.js" ></script>

<body>
<!--container start-->
<div class= "Container" style= "height:3000px; width:3000px; " >

<a class= "btn" href= "javascript:;" > Click </a>
</div>
<!--container end-->

<script type= "Text/javascript" >
/*window.onload = function () {
Window.Scroll (0,300);

$ (". Btn"). On ("click", Function () {

Document.documentElement.scrollLeft = "500";
var otop = Document.body.scrollTop | | Document.documentElement.scrollTop;
var oleft = Document.body.scrollLeft | | Document.documentElement.scrollLeft;

alert (oleft);
});
}*/

$ (function () {
$ ("Html,body"). Animate ({"ScrollLeft": "300px"}, 1000);
});

</script>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.