当我更改函数的位置时,为什么不使用javascript闭包?

时间:2018-06-26 03:58:06

标签: javascript html css

我是一个新的javascript,让我直接讲一下。我听说我们必须尽可能避免使用全局变量,因此,我学会了使用闭包来确保变量之间不会相互访问。

我在顶部具有slideShow功能,在底部具有toggleMenu功能,这些功能在我的网页上都可以正常使用。

我的问题是,当我将函数的位置更改为以下位置时,将slideShow函数更改为底部,将toggleMenu函数更改为顶部,我的网页看起来不正常,无法正常工作

也许有些我不知道的东西?还是我做错了什么?如果有人可以给我一些建议,我将不胜感激,谢谢。

这是原始代码

var slideShow = (function () {
    var slideImages = document.getElementsByClassName("slide");
    var leftSide = document.getElementById("arrow-left");
    var rightSide = document.getElementById("arrow-right");
    var slideBullets = document.getElementsByClassName("bullets");
    var current = 0;

    function reset() {
        for (let i = 0; i < slideImages.length; i++) {
            slideImages[i].style.display = 'none';
            slideBullets[i].classList.remove("clicked");
        }
    };

    function showImages() {
        for (let i = 0; i < slideImages.length; i++) {
            slideImages[0].style.display = 'block';
            slideBullets[current].classList.add("clicked");
        }
    };

    function arrowSlide() {
        leftSide.addEventListener("click", function () {
            reset();
            if (current === 0) {
                current = slideImages.length;
            }
            slideImages[current - 1].style.display = 'block';       
            current--;
            slideBullets[current].classList.add("clicked");
        });

        rightSide.addEventListener("click", function () {
            reset();
            if (current === slideImages.length - 1) {
                current = - 1;
            }
            slideImages[current + 1].style.display = 'block';         
            current++;
            slideBullets[current].classList.add("clicked");
        });
    };

    function showBulletsImages() {
        for (let i = 0; i < slideBullets.length; i++) {
            slideBullets[i].addEventListener("click", function () {
                reset();
                slideImages[i].style.display = 'block';
                slideBullets[i].classList.add("clicked");
                current = i;
            });
        }
    };

    function autoSlide() {
        setInterval(function () {
            rightSide.click();
            slideBullets[current].classList.add('clicked')
        }, 2000);
    };

    return {
        reset: reset(),
        showImages: showImages(),
        arrowSlide: arrowSlide(),
        showBulletsImages: showBulletsImages(),
        autoSlide: autoSlide()
    };
})();

var toggleMenu = (function () {
    var mainTopics = document.getElementById("maintopics");

    mainTopics.addEventListener("click", function (e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        mainTopics.classList.toggle("show");
    });

    document.addEventListener("click", function (e) {
        if (e.target.id !== 'arrow-right') {
            mainTopics.classList.remove("show");
        }
    });

    return {
        toggleMenu: toggleMenu()
    };
})();
body {
    margin: 0;
}

li, a{
    text-decoration: none;
    list-style-type: none;
    text-decoration-line: none;
    color: black;
}

/*main-menu*/
#mainmenu {
    position: relative;
}

#mainmenu ul {
    margin: 0;
    padding: 0;
}

#mainmenu li {
    display: inline-block;
}

#mainmenu a {
    display: block;
    width: 100px;
    padding: 10px;
    border: 1px solid;
    text-align: center;
}

/*sub-topics*/
#subtopics {
    position: absolute;
    display: none;
    margin-top: 10px;
    width: 100%;
    left: 0;
}

#maintopics.show #subtopics {
    display: block;
}

#subtopics ul {
    margin: 0;
    padding: 0;
}

#subtopics li {
    display: block;
}

#subTopics a {
    text-align: left;
}

/*columns*/
#column1, #column2, #column3 {
    position: relative;
    float: left;
    left: 125px;
    margin: 0px 5px 0px 0px;
}

/*hover underline*/
#mainmenu li:hover {
    text-decoration: underline;
}

/*slideshow*/
#slideshow {
    position: relative;
    width: 100%;
    height: 100%;
}

#slide1 {
    background-image: url(https://preview.ibb.co/mV3TR7/1.jpg);
}

#slide2 {
    background-image: url(https://preview.ibb.co/bSCBeS/2.jpg);
}

#slide3 {
    background-image: url(https://preview.ibb.co/kgG9Yn/3.jpg);
}

.slide {
    background-repeat: no-repeat;
    background-position: center;
    background-size: 800px 400px;
    width: 800px;
    height: 400px;
    margin: auto;
    margin-top: 40px;
}

.slide-contain {
    position: absolute;
    left: 50%;
    bottom: 50%;
    transform: translate3d(-50%,-50%,0);
    text-align: center;
}

.slide-contain span {
    color: white;
}

/*arrow*/
.arrow {
    position: absolute;
    cursor: pointer;
    top: 200px;
    width: 0;
    height: 0;
    border-style: solid;
}

.arrow:hover {
    background-color: #e0dede;
    transition: background-color 0.6s ease;
}


#arrow-left {
    position: absolute;
    border-width: 30px 40px 30px 0px;
    border-color: transparent gray transparent transparent;
    left: 0;
    margin-left: 300px;
}


#arrow-right {
    border-width: 30px 0px 30px 40px;
    border-color: transparent transparent transparent gray;
    right: 0;
    margin-right: 300px;
}

/*bullets*/
#slidebullet {
    position: relative;
    top: -30px;
    text-align: center;
}

.bullets {
    display: inline-block;
    background-color: gray;
    width: 15px;
    height: 15px;
    border-radius: 10px;
    cursor: pointer;
    transition: background-color 0.6s ease;
}

.clicked {
    background-color: #ff0000;
}
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" type="text/css" href="index.css" />
</head>
<body>
    <header></header>
    <nav>
        <div id="mainmenu">
            <ul>
                <li><a href="">Logo</a></li>
                <li><a href="">Home</a></li>
                <li id="maintopics"><a href="">Topics</a>
                    <div id="subtopics">
                        <div id="column1" class="columns">
                            <ul>
                                <li><a href="">example1</a></li>
                                <li><a href="">example2</a></li>
                            </ul>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
    </nav>
    <div id="slideshow">

        <div id="slide1" class="slide">
            <div class="slide-contain">
                <span>Image One</span>
            </div>
        </div>

        <div id="slide2" class="slide">
            <div class="slide-contain">
                <span>Image Two</span>
            </div>
        </div>

        <div id="slide3" class="slide">
            <div class="slide-contain">
                <span>Image Three</span>
            </div>
        </div>

        <div id="slidebullet">
            <div id="bullet1" class="bullets"></div>
            <div id="bullet2" class="bullets"></div>
            <div id="bullet3" class="bullets"></div>
        </div>

        <div id="arrow-left" class="arrow"></div>
        <div id="arrow-right" class="arrow"></div>
    </div>
    <script src="jquery.js"></script>
    <script src="index.js"></script>
    <script>

    </script>
</body>
</html>

然后我更改了功能的位置

var toggleMenu = (function () {
var mainTopics = document.getElementById("maintopics");

mainTopics.addEventListener("click", function (e) {
    e.preventDefault();
    e.stopImmediatePropagation();
    mainTopics.classList.toggle("show");
});

document.addEventListener("click", function (e) {
    if (e.target.id !== 'arrow-right') {
        mainTopics.classList.remove("show");
    }
});

return {
    toggleMenu: toggleMenu()
};
})();

var slideShow = (function () {
var slideImages = document.getElementsByClassName("slide");
var leftSide = document.getElementById("arrow-left");
var rightSide = document.getElementById("arrow-right");
var slideBullets = document.getElementsByClassName("bullets");
var current = 0;

function reset() {
    for (let i = 0; i < slideImages.length; i++) {
        slideImages[i].style.display = 'none';
        slideBullets[i].classList.remove("clicked");
    }
};

function showImages() {
    for (let i = 0; i < slideImages.length; i++) {
        slideImages[0].style.display = 'block';
        slideBullets[current].classList.add("clicked");
    }
};

function arrowSlide() {
    leftSide.addEventListener("click", function () {
        reset();
        if (current === 0) {
            current = slideImages.length;
        }
        slideImages[current - 1].style.display = 'block';       
        current--;
        slideBullets[current].classList.add("clicked");
    });

    rightSide.addEventListener("click", function () {
        reset();
        if (current === slideImages.length - 1) {
            current = - 1;
        }
        slideImages[current + 1].style.display = 'block';         
        current++;
        slideBullets[current].classList.add("clicked");
    });
};

function showBulletsImages() {
    for (let i = 0; i < slideBullets.length; i++) {
        slideBullets[i].addEventListener("click", function () {
            reset();
            slideImages[i].style.display = 'block';
            slideBullets[i].classList.add("clicked");
            current = i;
        });
    }
};

function autoSlide() {
    setInterval(function () {
        rightSide.click();
        slideBullets[current].classList.add('clicked')
    }, 2000);
};

return {
    reset: reset(),
    showImages: showImages(),
    arrowSlide: arrowSlide(),
    showBulletsImages: showBulletsImages(),
    autoSlide: autoSlide()
};
})();

1 个答案:

答案 0 :(得分:0)

问题似乎出在此

return {
    toggleMenu: toggleMenu()
};

toggleMenu立即调用函数中。该IIFE没有任何toggleMenu函数

除了像这样的回报

return {
    reset: reset(),
    showImages: showImages(),
    arrowSlide: arrowSlide(),
    showBulletsImages: showBulletsImages(),
    autoSlide: autoSlide()
};

不合适,用以下内容替换 slideShow没有理由要参加IIFE

return {
    reset: reset,
    showImages: showImages,
    arrowSlide: arrowSlide,
    showBulletsImages: showBulletsImages,
    autoSlide: autoSlide
};

这是stackblitz上的有效演示