JavaScript picture sliding effect realizes _javascript skill

Source: Internet
Author: User

This article for everyone to share the JavaScript image sliding effect implementation method, the specific contents are as follows, first look at the effect chart:

The mouse over the picture, show the complete which picture, remove is reset:

Simple CSS Plus JS operation DOM implementation:

<!doctype html>
 
 

Css:

#container {
 height:477px;
 margin:0 Auto;
 border-right:1px solid #ccc;
 border-bottom:1px solid #ccc;
 Overflow:hidden;
 position:relative;
}

#container img {
 position:absolute;
 Display:block;
 left:0;
 border-left:1px solid #ccc;
}

JS Operation:

Window.onload = function () {//container object var box = document.getElementById (' container ');

 Get Picture NodeList object Set var IMGs = Box.getelementsbytagname (' img ');

 The width of the single picture var imgwidth = imgs[0].offsetwidth;

 Set Hidden door body exposed width var exposewidth = 180;
 Set the total width of the container var boxwidth = ImgWidth + (imgs.length-1) * exposewidth;

 Box.style.width = boxwidth + ' px '; Set the initial position of each door function Setimgspos () {for (var i = 1, len = Imgs.length i < len; i++) {imgs[i].style.left = IMGW
  Idth + exposewidth * (i-1) + ' px ';

 } setimgspos ();

 Calculate the distance each door should be moved when it is opened var translate = Imgwidth-exposewidth; Bind event for each door for (var i = 0, len = imgs.length i < len; i++) {//Use the immediately called function table answer, in order to get a different I-value (function (i) {IMGS[I].O
    Nmouseover = function () {//reset each door first setimgspos (); 
     Open the door for (var j = 1; J <= I; j +) {Imgs[j].style.left = parseint (Imgs[j].style.left, ten)-translate + ' px ';
    Imgs[j].style.left = j*exposewidth + "px";
   }
   };
 Imgs[i].onmouseout = function () {   Setimgpos ();
  };
 }) (i); 
 }
};

Hopefully this article will help you learn about JavaScript programming.

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.