js 图片无缝循环

<html>
<head>
<title>Js图片无缝滚动</title>
<style type="text/css">
body{
    background:gray;
}
#wrap{
    width:810px;
    height:200px;
    border:1px solid black;
    position:relative;
    left:50%;
    top:50%;
    margin-left:-410px;
    margin-top:-250px;
    background:#F5E0E3;
    overflow:hidden;
}
#wrap ul{
    margin:0px;
    padding:0px;
    position:absolute;
    top:0px;
    left:0px;
}
#wrap ul li{
    list-style:none;
    float:left;
    margin:5px 10px;
}
#wrap ul li img{
    width:250px;
    height:180px;
    cursor:pointer;
}
.direction{
    width:104px;
    margin:50px auto;
}
.direction img{
    border:1px dotted pink;
    cursor:pointer;
}
.active{
    border:2px solid gray;
}
</style>
<script type="text/javascript">
window.onload = function(){
    var ul = document.getElementById("wrap").getElementsByTagName("ul")[0];
    var lis = ul.getElementsByTagName("li");
    var btn1 = document.getElementById("button1");
    var btn2 = document.getElementById("button2");
    var imgs = ul.getElementsByTagName("img");
    var speed = 3;
    var time = null;
    ul.innerHTML += ul.innerHTML;
    ul.style.width = (lis[0].offsetWidth+20)*lis.length + "px";
    time = setInterval(function(){
        ul.style.left = ul.offsetLeft - speed + "px";
        if(ul.offsetLeft <= -ul.offsetWidth/2){
            ul.style.left ="0px";
        }else if(ul.offsetLeft >=0){
            ul.style.left = -ul.offsetWidth/2 + "px";
        }
    },30);
    btn1.onmouseover = function(){
        speed = 3;
    }
    btn2.onmouseover = function(){
        speed = -3;
        }
    for(var i in imgs){
        imgs[i].onmouseover = function(){
            clearInterval(time);
            for(var i = 0;i<imgs.length;i++){
                    imgs[i].className = "none";
                }
            this.className = "active";
        }
        imgs[i].onmouseout = function(){
            time = setInterval(function(){
                ul.style.left = ul.offsetLeft - speed + "px";
                if(ul.offsetLeft <= -ul.offsetWidth/2){
                    ul.style.left ="0px";
                }else if(ul.offsetLeft >=0){
                    ul.style.left = -ul.offsetWidth/2 + "px";
                }
            },30);
            for(var i = 0;i<imgs.length;i++){
                imgs[i].className = "none";
            }
        }
    }
}
</script>
</head>
<body>
    <div class="direction"><img src="/jscss/demoimg/201204/left.png"  id="button1"/><img src="/jscss/demoimg/201204/right.png"  id="button2"/></div>
    <div id="wrap">
        <ul>
            <li><img src="/jscss/demoimg/wall1.jpg" alt="pic one" /></li>
            <li><img src="/jscss/demoimg/wall2.jpg" alt="pic two" /></li>
            <li><img src="/jscss/demoimg/wall3.jpg" alt="pic three" /></li>
            <li><img src="/jscss/demoimg/wall4.jpg" alt="pic four" /></li>
        </ul>
    </div>
</body>
</html>

js 图片无缝循环

时间: 2024-10-23 12:06:15

js 图片无缝循环的相关文章

超简单的纯CSS图片无缝循环方法

首先效果如下. 效果就是若干长图通过视口,并且第一张和最后一张要无缝衔接. 接着原理图如下.真的,超简单... 注意:第一张和最后一张必须要是同一张图,这样才能无缝衔接. 如果视口大于每张图片,那就有必要第1,2甚至3张和倒数第3,2,1张相同,这样才能在视口大于图片的情况下完成完成衔接. 从右往左滚动,或者垂直方向滚动效果稍加变动代码即可实现. 代码如下 <!DOCTYPE html> <html> <head lang="en"> <met

JS -- 图片无缝轮滚

1 <!DOCTYPE html> 2 <html lang="cn"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <style type="text/css"> 7 #box{ 8 width: 800px; 9 height: 200px; 10 border: 1px soli

js图片无缝滚动代码

想必大家都注意到<marquee>的不循环滚动,所以出现了很多替代脚本,或iframe或JS输出<marquee>,不管怎么做,都略显麻烦.下面说一下这个相对简单的实现思路:一个设定宽度并且隐藏超出它宽度的内容的容器demo,里面放demo1和 demo2,demo1是滚动内容,demo2为demo1的直接克隆,通过不断改变demo1的scrollTop或者scrollLeft达到滚动的目的,当滚动至demo1与demo2的交界处时直接跳回初始位置,因为demo1与demo2一样,

js实现无缝循环滚动

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>无缝滚动</title> </head> <style type="text/css"> .row{ width: 1298px; height: 400px; border: 1px solid; box-sha

js图片无缝滚动

<html> <head> <title>图片滚动</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <style> #demo { background:#fff; overflow: hidden; width:500px; border: 1px solid #ccc; } #demo

常用JS图片滚动(无缝、平滑、上下左右滚动)代码大全

常用JS图片滚动(无缝.平滑.上下左右滚动)代码大全 今天刚网上看的 不多说直接帖代码---- <head><-----></head><body> <!--向下滚动代码开始--><div id="colee" style="overflow:hidden;height:253px;width:410px;"><div id="colee1"><p>&

图片切换特效(2):JS图片滚动代码(无缝、平滑)

转自:http://www.codefans.net/jscss/code/255.shtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">

用js动态的改变img标签里面的src属性实现图片的循环切换

JS:根据循环切换的条件可以用 document.getElementById('').src=''设置, 或者jquery方法: $('#id').attr('src','图片名称’): 具体: if(true) { document.getElementById('id').src='a.png'; //or $('#id').attr('src','a.png'); }else { document.getElementById('id').src='b.png'; //or $('#id

JS可控制的图片自动循环播放查看效果

JS可控制的图片自动循环播放查看效果 <html> <head> <title>JS可控制的图片自动循环播放查看效果丨芯晴网页特效丨CsrCode.Cn</title> </head> <body> <SCRIPT LANGUAGE="JavaScript"> <!-- Begin var rotate_delay = 5000; // delay in milliseconds (5000 =