>  기사  >  웹 프론트엔드  >  JS 스위치 사진 슬라이드쇼 전환 효과

JS 스위치 사진 슬라이드쇼 전환 효과

韦小宝
韦小宝원래의
2017-12-04 10:03:312592검색

이 글에서는 js를 사용하여 사진 전환을 위한 슬라이드쇼 효과를 만드는 방법을 자세히 설명합니다. js코드의 모든 줄이 설명되어 있습니다. js에 익숙하지 않은 학생들을 위한 혜택입니다. 자세한 내용을 읽어보세요!

코드:

<!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"><head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>JS切换图片幻灯切换效果</title>
    <style>
        body, p, ul, li { margin: 0; padding: 0; }
        ul { list-style-type: none; }
        body { background: #000; text-align: center; font: 12px/20px Arial; }
        #box { position: relative; width: 322px; height: 172px; background: #fff; border-radius: 5px; border: 8px solid #fff; margin: 10px auto; }
        #box .list { position: relative; width: 320px; height: 240px; overflow: hidden; border: 1px solid #ccc; }
        #box .list li { position: absolute; top: 0; left: 0; width: 320px; height: 240px; opacity: 0; filter: alpha(opacity=0); }
        #box .list li.current { opacity: 1; filter: alpha(opacity=100); }
        #box .count { position: absolute; right: 0; bottom: 5px; }
        #box .count li { color: #fff; float: left; width: 20px; height: 20px; cursor: pointer; margin-right: 5px; overflow: hidden; background: #F90; opacity: 0.7; filter: alpha(opacity=70); border-radius: 20px; }
        #box .count li.current { color: #fff; opacity: 1; filter: alpha(opacity=100); font-weight: 700; background: #f60; }
        #tmp { width: 100px; height: 100px; background: red; position: absolute; }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            var oBox = document.getElementById("box");            var aUl = document.getElementsByTagName("ul");            var aImg = aUl[0].getElementsByTagName("li");            var aNum = aUl[1].getElementsByTagName("li");            var timer = play = null;            var i = index = 0;            var bOrder = true;            //切换按钮
            for(i = 0; i < aNum.length; i++) {
                aNum[i].index = i;
                aNum[i].onmouseover = function() {
                    show(this.index)
                }
            }            //鼠标划过关闭定时器
            oBox.onmouseover = function() {
                clearInterval(play)
            };            //鼠标离开启动自动播放
            oBox.onmouseout = function() {
                autoPlay()
            };            //自动播放函数
            function autoPlay() {
                play = setInterval(function() {
                    //判断播放顺序
                    bOrder ? index++ : index--;                    //正序
                    index >= aImg.length && (index = aImg.length - 2, bOrder = false);                    //倒序
                    index <= 0 && (index = 0, bOrder = true);                    //调用函数
                    show(index)
                }, 2000);
            }

            autoPlay();//应用
            function show(a) {
                index = a;                var alpha = 0;                for(i = 0; i < aNum.length; i++)aNum[i].className = "";
                aNum[index].className = "current";
                clearInterval(timer);                for(i = 0; i < aImg.length; i++) {
                    aImg[i].style.opacity = 0;
                    aImg[i].style.filter = "alpha(opacity=0)";
                }
                timer = setInterval(function() {
                    alpha += 2;
                    alpha > 100 && (alpha = 100);
                    aImg[index].style.opacity = alpha / 100;
                    aImg[index].style.filter = "alpha(opacity = " + alpha + ")";
                    alpha == 100 && clearInterval(timer)
                }, 20);
            }
        };    </script></head><body><p id="box">
    <ul class="list">
        <li class="current"><img src="img/3.jpg" width="320" height="240"/></li>
        <li><img src="img/1.jpg" width="320" height="240"/></li>
        <li><img src="img/2.jpg" width="320" height="240"/></li>
        <li><img src="img/3.jpg" width="320" height="240"/></li>
        <li><img src="img/4.jpg" width="320" height="240"/></li>
    </ul>
    <ul class="count">
        <li class="current">1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
    </ul></p></body></html>

위 내용은 JS 사진 전환 슬라이드쇼 전환 효과의 모든 내용입니다. 학생들에게 도움이 되었으면 좋겠습니다!

관련 권장사항:

JS 슬라이드쇼는 스크롤 탐색(자체 작성)_javascript 기술로 원활하게 반복 및 회전할 수 있습니다. 무작위로 전환되는 배경 이미지를 설정하려면

위 내용은 JS 스위치 사진 슬라이드쇼 전환 효과의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.