原生幻燈片封裝學習

来源:https://www.cnblogs.com/xueyunNO1/archive/2018/11/02/9896509.html
-Advertisement-
Play Games

JavaScript部分 HTML結構部分 CSS部分 ...


JavaScript部分

(function(window,document) {
    var Broadcast = function(cla,options) {
        // 判斷是用函數創建的還是用new創建的。這樣我們就可以通過Broadcast("dom") 或 new Broadcast("dom")來使用
        if (!(this instanceof Broadcast)) return new Broadcast(cla, options);
        // 排序圖片的層疊位置
        var front = this.nameclass(cla),//獲取的class元素組
            img = this.childer(front),
            imglength = img.length,
            jishu = 0,
            _this = this,intr;
        for (var r in img) {
            if (r == 0) {
                img[r].style.cssText= 'z-index:' + (imglength - r) + ';position:absolute;opacity:1;transition:0.5s;';
            } else {
                img[r].style.cssText= 'z-index:' + (imglength - r) + ';position:absolute;opacity:0;transition:0.5s;';
            }
        }
        //設置選擇器 
        if(options.spot=="true"){
                if(options.Selector!=undefined){
                    for(var i=0;i<imglength;i++){
                        var domspan = document.createElement('span');
                        if(i==0){
                            domspan.setAttribute('class',options.current)
                        }
                        this.nameclass(options.Selector)[0].appendChild(domspan)
                    } 
                }
        }
        var spotdom = this.nameclass(options.Selector)[0].children;
        // 給選擇器添加點擊事件
        for(var y=0;y<spotdom.length;y++){
            spotdom[y].index = y;
            if(y!=0){
                spotdom[y].setAttribute('class','');
            }
            spotdom[y].onclick=function(){
               this.setAttribute('class',options.current)
               jishu=this.index;
               _this.Front(img,jishu,spotdom);
            }
        }
         // 給兩個箭頭綁定點擊事件
        if(options.leftarrow!=undefined&&options.rightarrow!=undefined){
            this.nameclass(options.leftarrow)[0].onclick=function(){
                if(jishu==0){
                    jishu=imglength-1
                }else{
                    jishu--
                }
                _this.Front(img,jishu,spotdom);
            }
            this.nameclass(options.rightarrow)[0].onclick=function(){
                if(jishu==imglength-1){
                    jishu=0;
                }else{
                    jishu++
                }
                _this.Front(img,jishu,spotdom);
            }
        }
        // 自動播放
        var bof = function(){
            intr = setInterval(function() {
            if(img.length-1==jishu){
                jishu=0;
                }else{
                    jishu++
                }
                _this.Front(img,jishu,spotdom)
            }, 2000);
        }
        bof();
        // 滑鼠經過停止
        front[0].onmouseover = function(){
            intr = clearInterval(intr);
        }
        // 滑鼠離開播放
        front[0].onmouseout = function(){
            bof();
        }
    }
    Broadcast.prototype = {
        nameclass: function(mingzi) { //獲取class元素組
            var obj = [];
            var jia = 0;
            var dom = document.getElementsByTagName('*');
            for (var i in dom) {
                if (dom[i].nodeType == 1) {
                    if (dom[i].getAttribute('class') === mingzi) {
                        obj[jia] = dom[i];
                        jia++
                    }
                }
            }
            return obj;
        },
        childer: function(father,name) { //獲取子集元素組
            var obj2 = [],
                j = 0;
            var dom = father[0].children[0].childNodes;

            for (var h in dom) {
                if (dom[h].nodeType == 1) {
                    obj2[j] = dom[h];
                    j++
                }
            }
            return obj2;
        },
        Front: function(dom2,cishu,xuan) {
            var dom2length = dom2.length;
            for (var r in dom2) {
                if (r == cishu) {
                    dom2[r].style.opacity='1';
                    xuan[r].setAttribute("class",'current-dan')
                } else {
                    dom2[r].style.opacity='0';
                    xuan[r].setAttribute("class",'')
                }
            }

        }
    }
    // 暴露方法
    window.Broadcast = Broadcast;
})(window, document);

HTML結構部分

<div class="boy">
        <div class="img-box">
            <a href="" title="" ><img src="img/timg(1).png" alt=""></a>
            <a href="" title="" ><img src="img/timg(2).png" alt=""></a>
            <a href="" title="" ><img src="img/timg(3).png" alt=""></a>
            <a href="" title="" ><img src="img/timg.png" alt=""></a>
        </div>
        <div class="selector"></div>
        <div class="left-att"><</div>
        <div class="right-att">></div>
    </div>

CSS部分

.boy{
    width: 1103px;
    margin: 0 auto;
    height: 714px;
    position: relative;
}
.img-box{
    height: 714px;
}
.img-box a{
    float: left;
}
/* 設置圓點的樣式 */
.selector{
    width: 96px;
    position: absolute;
    left: 50%;
    margin-left: -48px;
    bottom: 20px;
    z-index: 222;
}
.selector span{
    float: left;
    margin-left: 10px;
    border-radius: 100px;
    background-color: #999;
    width: 14px;
    height: 14px;
    cursor: pointer;
}
.selector .current-dan{
    background-color: #13af13;
}
/* 設置箭頭的位置 */
.left-att{
    position: absolute;
    left: 20px;
    margin-top: -20px;
    top: 50%;
    width: 40px;
    height: 40px;
    background: rgba(0, 0, 0, 0.5);
    color: #fff;
    text-align: center;
    line-height: 40px;
    font-weight: bold;
    z-index: 222;
    cursor: pointer;
}
.right-att{
    position: absolute;
    right: 20px;
    margin-top: -20px;
    top: 50%;
    width: 40px;
    height: 40px;
    background: rgba(0, 0, 0, 0.5);
    color: #fff;
    text-align: center;
    line-height: 40px;
    font-weight: bold;
    cursor: pointer;
    z-index: 222;
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 做升級的時候,把更新包拷貝到sd卡中,然後調用介面進行重啟升級 "wossoneri.github.io" 之後進入Recovery模式後報錯: 說是找不到 這個路徑? 因為上層用Java寫路徑的時候,獲取的是Android的路徑,我們知道,adb shell裡面是有 的路徑的,這個路徑實際上並不是 ...
  • 1.html: 2.vue的methods 裡面 ...
  • elementUI vue table 操作選項中彈出框確定dialog ...
  • 網上的相關教程非常多,基礎知識自行搜索即可。 習題主要選自Orelly出版的《數據結構與演算法javascript描述》一書。 參考代碼可見: "https://github.com/dashnowords/blogs/tree/master/Structure/Hash" 散列的基本知識 定義 哈希 ...
  • function getseat(){ var option= "<button class='btn'style='margin:5px;' onclick='onclickSeat(this)' id='全部'>全部</a>" url = "CarGetSeat"; $.post(url,nul ...
  • 一般創建對象是用以下兩種方式 new object創建對象: 或者,對象字面量的方式: 這兩種方式的缺點是:同一個介面創建很多對象,會產生大量的重覆代碼,如var Person1={},var Person2={},為瞭解決這個問題,人們開始使用工廠模式的一種變體來創建對象。 一、工廠模式 由於在E ...
  • 改了一個小項目,裡面有一個小的問題他們是這樣提需求的。兩個輸入框,第一個輸入框裡面,輸入的內容會對第二個輸入框中的內容產生影響。具體是這樣的:如果第一個輸入框中的值不是“0”,那麼第二個輸入框就不能填寫任何的數據,只能預設“0”。只有當第一個輸入框中輸入0的時候,第二個輸入框才是可以輸入的。這個需求 ...
  • 大多數同學在模仿慕課網的時候可能會遇到引入bootstrap和jquery樣式部分失效以及$符號報錯,這裡為大家提供正確的解決方案。 可能大家在引入試過col-md之後覺得bootstrap是ok的,其實這個navbar-inverse不顯示黑色的問題在於bootstrap3.0和4.0之間的更新, ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...