Javascript image slide effect, javascript image slide

Source: Internet
Author: User

Javascript image slide effect, javascript image slide

This article shares with you how to implement javascript image sliding. The specific content is as follows:

Move the mouse over the image to show the complete image. If you remove the image, reset it:

Simple CSS and JS operations 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 operations:

Window. onload = function () {// container object var box = document. getElementById ('Container'); // get the image NodeList object set var imgs = box. getElementsByTagName ('img '); // The width of a single image var imgWidth = imgs [0]. offsetWidth; // set var exposeWidth = 180 to hide the width exposed by the door; // set the total width of the container var boxWidth = imgWidth + (imgs. length-1) * exposeWidth; box. style. width = boxWidth + 'px '; // you can specify the initial position of each gate. function setImgsPos () {for (var I = 1, len = imgs. length; I <len; I ++) {imgs [I]. style. left = imgWidth + exposeWidth * (I-1) + 'px ';}} setImgsPos (); // calculates the distance from var translate = imgWidth-exposeWidth when each door is opened; // bind the event for (var I = 0, len = imgs. length; I <len; I ++) {// use the answer of the function table called immediately to obtain different I values (function (I) {imgs [I]. onmouseover = function () {// reset each door to setImgsPos (); // open the door for (var j = 1; j <= I; j ++) {imgs [j]. style. left = parseInt (imgs [j]. style. left, 10)-translate + 'px '; // imgs [j]. style. left = j * exposeWidth + "px" ;}}; imgs [I]. onmouseout = function () {setImgPos () ;}}) (I );}};

I hope 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.