原生幻燈片封裝學習

来源: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
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...