javascript:100% native JS implementation of the left and right switch Carousel diagram (no delay loading)

Source: Internet
Author: User
Tags object object setinterval

<!--
Description: this. html file must have:
(1) The Json,json folder must have the file Data.txt, the contents of the file Data.txt are:
[{"Imgsrc": "Img/banner1.jpg"},
{"IMGSRC": "Img/banner2.jpg"},
{"IMGSRC": "Img/banner3.jpg"},
{"IMGSRC": "Img/banner4.jpg"}]
(2) The following images must be in the Img,img folder of the sibling folder: Banner1.jpg;banner2.jpg;banner3.jpg;banner4.jpg;
-
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<style>
*{
margin:0;
padding:0;
List-style:none;
}
. box{
width:1000px;
height:300px;
position:relative;
margin:30px Auto;
Overflow:hidden;
}
. box. boxinner{
width:4000px;
Position:absolute;
left:0;
top:0;
}
. box. Boxinner div{
width:1000px;
height:300px;
Float:left;
}
. box. Boxinner Div img{
width:100%;
height:100%;
}
. Box ul{
Position:absolute;
right:10px;
bottom:10px;
}
. Box UL li{
width:18px;
height:18px;
Background: #ccc;
border-radius:50%;
margin-left:10px;
Float:left;
Cursor:pointer;
}
. Box UL li.on{
Background:lightblue;
}
. Box a{
width:30px;
height:30px;
Position:absolute;
top:127px;
border:10px solid red;
Border-left:none;
Border-bottom:none;
opacity:0.3;
Filter:alpha (opacity=30);
Display:none;
}
. Box a:hover{
Opacity:1;
Filter:alpha (opacity=100);
}
. box. btnleft{
Transform:rotate ( -135DEG);
left:30px;
}
. box. btnright{
Transform:rotate (45DEG);
right:30px;
}

</style>
<body>
<div class= "box" id= "box" >
<div class= "Boxinner" >
<!--<div></div>
<div></div>
<div></div>
<div></div>-->
</div>
<ul>
<!--<li class= "on" ></li>
<li></li>
<li></li>
<li></li>-->
</ul>
<a href= "javascript:;" class= "Btnleft" ></a>
<a href= "javascript:;" class= "Btnright" ></a>
</div>
<script>
function Getcss (curele,attr) {
var val=null;
var reg=null;
if (getComputedStyle) {//Standard
Val=getcomputedstyle (Curele,false) [attr];
}else{//Non-standard
if (attr=== ' opacity ') {
Val=curele.currentstyle.filter; ' Alpha (opacity=10) '
Reg=/^alpha\ (opacity[=:] (\d+) \) $/i;
Return Reg.test (val)? Reg.exec (Val) [1]/100:1;
}
VAL=CURELE.CURRENTSTYLE[ATTR];
}
Reg=/^[+-]? ((\d| ([1-9]\d+)) (\.\d+)?) (Px|pt|rem|em) $/i;
Return Reg.test (val)? parseint (val): Val;
}
function Setcss (curele,attr,value) {
if (attr=== ' float ') {
Curele.style.cssfloat=value;
Curele.style.stylefloat=value;
Return
}
if (attr=== ' opacity ') {
Curele.style.opacity=value;
Curele.style.filter= ' alpha (opacity= ' + (value*100) + ') ';
Return
}
Handling of the Unit
var reg=/^ (width|height|top|right|bottom|left| ( (margin|padding) (Top|right|bottom|left))) $/i;
if (Reg.test (attr)) {
if (! ( value=== ' Auto ' | | Value.tostring (). IndexOf ('% ')!==-1)) {
Value=parsefloat (value) + ' px ';
}
}
Curele.style[attr]=value;
}
function Setgroupcss (curele,opt) {
if (opt.tostring ()!== ' [Object Object] ') return;
for (var attr in opt) {
This.setcss (curele,attr,opt[attr]);
}
}
function css (Curele) {
var arg2=arguments[1];
if (typeof arg2=== ' string ') {
var arg3=arguments[2];
if (typeof arg3=== ' undefined ') {//When the third parameter does not exist, is obtained;
Return This.getcss (CURELE,ARG2);
}else{
This.setcss (CURELE,ARG2,ARG3);
}
}
if (arg2.tostring () = = = ' [Object Object] ') {//Get a set of elements
This.setgroupcss (CURELE,ARG2);
}
}
function Animate (curele,target,duration) {
function Tmpeffect (t, B, C, D) {
Return c * t/d + B;
}
1. Prepare for each parameter of the formula
var begin={};
var change={};
for (var attr in target) {
Begin[attr]=css (CURELE,ATTR);
CHANGE[ATTR]=TARGET[ATTR]-BEGIN[ATTR];
}
duration=duration| | : 0];
var time=0;
2. Turn on a timer to keep the time accumulating, and to find the latest position according to the time and formula;
Clearinterval (Curele.timer); Turn off the useless timer before starting a timer.
Curele.timer=setinterval (function () {
time+=10;
3. Conditions for cessation of movement (time>=duration)
if (time>=duration) {
CSS (curele,target);
Clearinterval (Curele.timer);
Return
}
Get the latest value for each attribute, and assign to the attribute corresponding to the element;
for (var attr in target) {
var curpos=tmpeffect (time,begin[attr],change[attr],duration);
CSS (Curele,attr,curpos);
}
},10)
}
(function () {
Get element
var Obox=document.getelementbyid ("box");
var oboxinner=obox.getelementsbytagname (' div ') [0];
var adiv=oboxinner.getelementsbytagname (' div ');
var oul=obox.getelementsbytagname (' ul ') [0];
var ali=oul.getelementsbytagname (' Li ');
var obtnleft=obox.getelementsbytagname (' a ') [0];
var obtnright=obox.getelementsbytagname (' a ') [1];
var data=null;
var timer=null;
var step=0;
1. Get and parse data
GetData ();
function GetData () {
var xml=new xmlhttprequest;
Xml.open (' Get ', ' json/data.txt ', false);
Xml.onreadystatechange=function () {
if (xml.readystate==4 &&/^2\d{2}$/.test (xml.status)) {
Data=json.parse (Xml.responsetext);
}
};
Xml.send ();
}
2. Binding Data
Bind ();
function bind () {
var strdiv= ';
var strli= ';
for (var i=0; i<data.length; i++) {
strdiv+= ' <div></div> ';
Strli+=i===0? ' <li class= "On" ></li> ': ' <li></li> ';
}
strdiv+= ' <div></div> ';
Oboxinner.innerhtml+=strdiv;
oboxinner.style.width=adiv.length*adiv[0].offsetwidth+ ' px ';
Oul.innerhtml+=strli;
}

3. Automatic Picture Carousel
Timer=setinterval (automove,1400);
function Automove () {
if (step>=adiv.length-1) {
Step=0;
CSS (Oboxinner, ' left ', 0)
}
step++;
Animate (oboxinner,{left:-step*1000});
Bannertip ();
}
4. Focus Auto-Carousel
function Bannertip () {
var tmpstep=step>=ali.length?0:step;
for (var i=0; i<ali.length; i++) {
Ali[i].classname=i==tmpstep? ' On ': null;
}
}

5. Mouse move stop, move out continue
Obox.onmouseover=function () {
Clearinterval (timer);
obtnleft.style.display= ' block ';
obtnright.style.display= ' block ';
};
Obox.onmouseout=function () {
Timer=setinterval (automove,1400);
Obtnleft.style.display= ' None ';
Obtnright.style.display= ' None ';
};
6. Click Focus to switch manually
HandleChange ();
function HandleChange () {
for (var i=0; i<ali.length; i++) {
Ali[i].index=i;
Ali[i].onclick=function () {
Step=this.index;
Animate (oboxinner,{left:-step*1000});
Bannertip ();
}
}
}
7. Click the left and right buttons to switch manually
Obtnright.onclick=automove;
Obtnleft.onclick=function () {
if (step<=0) {
Step=adiv.length-1;
CSS (Oboxinner, ' left ',-step*1000);
}
step--;
Animate (oboxinner,{left:-step*1000});
Bannertip ();
}
})();
</script>
</body>

javascript:100% native JS implementation of the left and right switch Carousel diagram (no delay loading)

Related Article

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.