JavaScript implements dynamic sidebar code _javascript tips

Source: Internet
Author: User

In general, the use of mouse hover onmouseover and mouse to remove onmouseout These two time to complete.

The first is the HTML structure

Copy Code code as follows:

<body>
<div id= "Div1" >
<span> Sidebar </span>
</div>
</body>

Then the style of the CSS:

Copy Code code as follows:

#div1 {
width:150px;
height:200px;
Background: #999999;
Position:absolute;
left:-150px;}
span{
width:20px;
height:70px;
line-height:23px;
Background: #09C;
Position:absolute;
right:-20px;
top:70px;}

The default style sidebar is hidden as pictured:

When the mouse moves into the following figure:

Here's the complete code:

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<style type= "Text/css" >
#div1 {
width:150px;
height:200px;
Background: #999999;
Position:absolute;
left:-150px;}
span{
width:20px;
height:70px;
line-height:23px;
Background: #09C;
Position:absolute;
right:-20px;
top:70px;}
</style>
<script>
Window.onload=function () {
var Odiv=document.getelementbyid (' Div1 ');
Odiv.onmouseover=function ()
{

Startmove (0,10); The first parameter is the target value of the div left property and the second is how many pixels to move each time

}
Odiv.onmouseout=function ()
{
Startmove ( -150,-10);
}
}

var timer=null;
function Startmove (target,speed)
{

var Odiv=document.getelementbyid (' Div1 ');
Clearinterval (timer);
Timer=setinterval (function () {

if (odiv.offsetleft==target)
{
Clearinterval (timer);
}
Else
{
odiv.style.left=odiv.offsetleft+speed+ ' px ';
}

},30)

}

</script>

<body>
<div id= "Div1" >
<span> Sidebar </span>
</div>
</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.