...”;然後根據自己的需求對css樣式進行更改;最後在需要實作輪播的地方加入js程式碼即可。"/> ...”;然後根據自己的需求對css樣式進行更改;最後在需要實作輪播的地方加入js程式碼即可。">

首頁  >  文章  >  CMS教程  >  phpcms如何實現輪播

phpcms如何實現輪播

藏色散人
藏色散人原創
2020-07-21 09:58:163214瀏覽
phpcms實現輪播的方法:先在想要加輪播圖的位置加入「
...
」;然後依照自己的需求對css樣式進行更改;最後在需要實作輪播的地方加入js程式碼即可。

phpcms如何實現輪播

phpcms首頁實作輪播圖

1.在你想要加輪播圖的位置加入以下

<div id="flowDiagram" >
          <div id="button">
            <span index="1" class="on"></span>
            <span index="2"></span>
            <span index="3"></span>
            <span index="4"></span>
              <span index="5"></span>
          </div>
          <div id="photo" style="left:-1200px;">
        <div>
           {pc:content  action="position" posid="1" thumb="1" order="listorder DESC" num="5"}
        {loop $data $r}
        <div ><a href="{$r[url]}" target="_blank" title="{$r[title]}"><img src="{thumb($r[thumb],1200,320)}"   style="max-width:90%" alt="{$r[title]}" /></a></div>
        {/loop}
        {/pc}
        <ul>
        {pc:content  action="lists" catid="" thumb="1" order="listorder DESC" num="5"}
        {loop $data $r}
        <li><a href="{$r[url]}" target="_blank" title="{$r[title]}">{str_cut($r[title],20)}</a></li>
        {/loop}
        {/pc}
        </ul>
        <div></div>
        </div>
        </div>
<span id="pre" class="arrow"> <</span>
<span id="next" class="arrow">> </span>
    </div>

由於這個焦點幻燈比較特殊,圖片和文字需要兩次調用,另外,後台添加內容時要勾選首頁焦點圖推薦,就可以添加到首頁

推薦:《 phpcms教學

2.當然,這裡面的css樣式根據自己的需求做更改,在這裡就不貼出css程式碼了,實作輪播需要加入以下js程式碼

window.onload=function(){//获取元素
    var flowDiagram = document.getElementById(&#39;flowDiagram&#39;);//容器
    var photo = document.getElementById("photo");
    var button = document.getElementById("button").getElementsByTagName(&#39;span&#39;);
    var pre = document.getElementById("pre");
    var next = document.getElementById("next");
    var index = 1;
    var m;

    function move(){
        m = setInterval(next.onclick,3000);
    }
    function stop(){
        if(m)clearInterval(m);
    }
    function buttonlight(){
        for (var i = 0; i < button.length; i++) {
            if(button[i].className == "on"){
                button[i].className = "";
                break;
            }
        }
        button[index-1].className = "on";
    }

    pre.onclick=function() {
        if (index == 1)
            index = 5;
        else
              index = index - 1;
        buttonlight();
        photo.style.left = parseInt(photo.style.left) + 1200 + "px";
        if (parseInt(photo.style.left) > -1200){
            photo.style.left = -6000 + "px";
       }
    }

    next.onclick = function(){//当right键被触发时响应
        if (index == 5)
            index = 1;
        else
            index = index + 1;
        buttonlight();
        photo.style.left = parseInt(photo.style.left) - 1200 + "px";
        if (parseInt(photo.style.left) < -6000){
            photo.style.left = -1200 + "px";
        }
    }

    for (var i = 0; i < button.length; i++){
        button[i].onclick = function()
        {
            if(this.className=="on")
                return;
            var currentindex = parseInt(this.getAttribute("index"));//getAttribute能获取自定义的属性值,也可以获取自带的属性值
            var distance = currentindex - index;
            photo.style.left = parseInt(photo.style.left) - 1200 * distance + "px";
            index = currentindex;
            buttonlight();
        }
    }
    flowDiagram.onmouseover = stop;
    flowDiagram.onmouseout = move;
    move();
}

最終效果

phpcms如何實現輪播

#

以上是phpcms如何實現輪播的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn