滚动时滑动门效果无法实现

来源:5-5 JS实现滑动门效果

MiMicccc

2020-11-19 21:21:54

# 具体遇到的问题
滑动门点击时效果可以有,但是在滚动页面时,导航条滑动效果无法显示

# 报错信息的截图
http://img.mukewang.com/climg/5fb67079094e93ae12780563.jpg

# 相关课程内容截图
回顾视频时发现我少打了代码

http://img.mukewang.com/climg/5fb670fd09a2985b06150451.jpg

少打得代码为注释的代码,但是加上的话会报错

http://img.mukewang.com/climg/5fb67133095aa9e609250144.jpg


# 粘贴全部相关代码,切记添加代码注释(请勿截图)

/*

1.页面载入完成后所有的动画元素设置_animate_init

2.页面滚动到某一屏 开始播放动画/导航条,大纲出现

3.导航条,大纲联动(双向定位-滚动导航条定位/大纲定位)

4.导航条滑动门特效

*/

// 1


// 获取元素

var getElem = function (selector) {

    return document.querySelector(selector);

}

var getAllElem = function (selector) {

    return document.querySelectorAll(selector);

}

// 获取元素的样式

var getCls = function (element) {

    return element.getAttribute('class');

}

// 设置元素的样式

var setCls = function (elementcls) {

    return element.setAttribute('class'cls)

}

// 为元素添加样式

var addCls = function (elementcls) {

    var baseCls = getCls(element);

    if (baseCls.indexOf(cls=== -1) {

        setCls(element, baseCls + " " + cls)

    }

    return;

}


// 为元素删除样式

var delCls = function (elementcls) {

    var baseCls = getCls(element);

    if (baseCls.indexOf(cls!= -1) {

        setCls(element, baseCls.split(cls).join(' ').replace(/\s+/g' '));

    }

    return;

}


// 1.初始化样式init


var screenAnimateElements = {

    '.screen-1': [

        '.screen-1__heading',

        '.screen-1__phone',

        '.screen-1__shadow'

    ],

    '.screen-2': [

        '.screen-2__heading',

        '.screen-2__subheading',

        '.screen-2__phone',

        '.screen-2__point_i_1',

        '.screen-2__point_i_2',

        '.screen-2__point_i_3',

    ],

    '.screen-3': [

        '.screen-3__heading',

        '.screen-3__phone',

        '.screen-3__subheading',

        '.screen-3__features',

    ],

    '.screen-4': [

        '.screen-4__heading',

        '.screen-4__subheading',

        '.screen-4__type__item_i_1',

        '.screen-4__type__item_i_2',

        '.screen-4__type__item_i_3',

        '.screen-4__type__item_i_4',

    ],

    '.screen-5': [

        '.screen-5__heading',

        '.screen-5__subheading',

        '.screen-5__bg',

    ]

}

function setScreenAnimateInit(screenCls) {

    var animateElements = screenAnimateElements[screenCls];// 获取需要设置动画的元素

    for (var i = 0; i < animateElements.length; i++) {

        var element = document.querySelector(animateElements[i]);

        var baseCls = element.getAttribute('class');

        // console.log(baseCls);

        element.setAttribute('class', baseCls + ' ' + animateElements[i].substr(1+ '_animate_init');

    }

}

window.onload = function () {

    // 为所有元素设置init

    for (k in screenAnimateElements) {

        // 优化让第一屏默认自动播放

        if (k === '.screen-1') {

            continue;

        }

        setScreenAnimateInit(k);

    }

    console.log('load');

}


// 2.滚动到哪就播放到哪

// 滚动条设置

function playScreenAnimateDone(screenCls) {

    var animateElements = screenAnimateElements[screenCls];// 获取需要设置动画的元素

    for (var i = 0; i < animateElements.length; i++) {

        var element = document.querySelector(animateElements[i]);

        var baseCls = element.getAttribute('class');

        element.setAttribute('class', baseCls.replace('_animate_init''_animate_done'));

    }

}


window.onscroll = function () {

    var top = document.documentElement.scrollTop || document.body.scrollTop;

    if (top > 100) {

        addCls(getElem('.header'), 'header_status_back');

        playScreenAnimateDone('.screen-1');

    } else {

        delCls(getElem('.header'), 'header_status_back');

        switchNavItemsActive(0);

    }

    if (top > 400) {

        addCls(getElem('.outline'), 'outline_status_in');

    }

    if (top > (800 * 1 - 100)) {

        playScreenAnimateDone('.screen-2');

        switchNavItemsActive(1)

    }

    if (top > (800 * 2 - 100)) {

        playScreenAnimateDone('.screen-3');

        switchNavItemsActive(2)


    }

    if (top > (800 * 3 - 100)) {

        playScreenAnimateDone('.screen-4');

        switchNavItemsActive(3)


    }

    if (top > (800 * 4 - 100)) {

        playScreenAnimateDone('.screen-5');

        switchNavItemsActive(4)


    }

}


// 3.导航条,大纲联动(双向定位-滚动导航条定位/大纲定位)

// 导航条点击页面跳转

var navItems = getAllElem('.header__nav-item');

var outLineItems = getAllElem('.outline__item');

//setNavJump的作用是给某个元素绑定点击事件,点击该元素时,页面跳到相应的位置。

//它的参数有两个,第一个元素的索引,第二个是元素所在的dom集合。

//在setNavJump方法内,通过“dom集合[索引]”就能获取的具体要给哪个元素添加点击事件:

//以如下代码为例:

var setNavJump = function (ilib) {

    var elem = lib[i];

    elem.onclick = function () {

        document.body.scrollTop = i * 800;

        console.log(elem);

    }

}

for (var i = 0; i < navItems.length; i++) {

    setNavJump(i, navItems);

}

for (var i = 0; i < outLineItems.length; i++) {

    setNavJump(i, outLineItems);

}


var switchNavItemsActive = function (idx) {

    // 给导航条添加active样式 

    for (var i = 0; i < navItems.length; i++) {

        delCls(navItems[i], 'header__nav-item_status_active');

        navTip.style.left = 0 + "px";

    }

    addCls(navItems[idx], 'header__nav-item_status_active');

    //navTip.style.left = (idx * 70) + "px";


    // 给大纲添加active样式

    for (var i = 0; i < outLineItems.length; i++) {

        delCls(outLineItems[i], 'outline__item_status_active');

        //outTip.style.top = 42 + "px";


    }

    addCls(outLineItems[idx], 'outline__item_status_active');

    //outTip.style.top = ((idx + 1) * 42) + "px";


}

switchNavItemsActive(0);


// 4.滑动门实现

var navTip = getElem(".header__nav-tip");

var outTip = getElem(".outline__nav-tip");

var setTip = function (idom) {

    dom[i].onmouseover = function () {

        console.log(i);

        navTip.style.left = (i * 70+ "px";

        outTip.style.top = ((i + 1* 42+ "px";

    }

    var current = 0;

    dom[i].onmouseout = function () {

        for (var i = 0; i < dom.length; i++) {

            if (getCls(dom[i]).indexOf('header__nav-item_status_active'!= -1) {

                current = i;

                break;

            }

            if (getCls(dom[i]).indexOf('outline__item_status_active'!= -1) {

                current = i;

                break;

            }

        }

        console.log(i);

        navTip.style.left = (current * 70+ "px";

        outTip.style.top = ((i + 1* 42+ "px";


    }


}

for (var i = 0; i < navItems.length - 1; i++) {

    setTip(i, navItems);

}

for (var i = 0; i < outLineItems.length; i++) {

    setTip(i, outLineItems);

}


// 优化让第一屏默认自动播放

setTimeout(function () {

    playScreenAnimateDone('.screen-1')

}, 200);


<html>


<head>

    <meta charset="utf-8">

    <title>慕课手机</title>

    <link rel="stylesheet" type="text/css" href="./css/base.css" />

    <link rel="stylesheet" type="text/css" href="./css/index.css" />

    <link rel="stylesheet" type="text/css" href="./css/animate.css" />

</head>


<body>

    <header class="header">

        <div class="header__wrap">

            <div class="header__logo">慕课手机</div>

            <nav class="header__nav">

                <a href="javascript:;" class="header__nav-item">首页</a>

                <a href="javascript:;" class="header__nav-item">外观</a>

                <a href="javascript:;" class="header__nav-item">配置</a>

                <a href="javascript:;" class="header__nav-item">型号</a>

                <a href="javascript:;" class="header__nav-item">说明</a>

                <a href="javascript:;" class="header__nav-item header__nav-item_custom_button">立即购买</a>

                <div class="header__nav-tip"></div>

            </nav>

        </div>

    </header>

    <div class="screen-1">

        <h2 class="screen-1__heading screen-1__heading_animate_init"><b>慕课手机</b> 让你的生活更精彩</h2>

        <div class="screen-1__phone screen-1__phone_animate_init"></div>

        <div class="screen-1__shadow screen-1__shadow_animate_init"></div>

    </div>

    <div class="screen-2">

        <h2 class="screen-2__heading">优美的设计,更令人着迷</h2>

        <h3 class="screen-2__subheading">采用受欢迎的设计,让它更加出色。<br>

            款式小巧、轻便手感更舒适。绚丽高清的显示屏,整个外观显得格外精致。</h3>

        <div class="screen-2__phone">

            <div class="screen-2__point screen-2__point_i_1">高清摄像</div>

            <div class="screen-2__point screen-2__point__custom_right screen-2__point_i_2">超薄机身</div>

            <div class="screen-2__point screen-2__point__custom_right screen-2__point_i_3">大屏显示</div>

        </div>

    </div>

    <div class="screen-3">

        <div class="screen-3__wrap">

            <h2 class="screen-3__heading">外形小巧,功能强大的手机</h2>

            <h3 class="screen-3__subheading">采用受欢迎的设计,让它更加出色。<br>

                款式小巧、轻便手感更舒适。绚丽高清的显示屏,整个外观显得格外精致。</h3>

            <div class="screen-3__phone"></div>

            <div class="screen-3__features">

                <div class="screen-3__features__item">

                    <div class="screen-3__features__item__number">5.7</div>

                    <div class="screen-3__features__item__decs">英寸大屏</div>

                </div>

                <div class="screen-3__features__item">

                    <div class="screen-3__features__item__number">1200</div>

                    <div class="screen-3__features__item__decs">万像素</div>

                </div>

                <div class="screen-3__features__item">

                    <div class="screen-3__features__item__number">3D</div>

                    <div class="screen-3__features__item__decs">touch</div>

                </div>

                <div class="screen-3__features__item">

                    <div class="screen-3__features__item__number">A9</div>

                    <div class="screen-3__features__item__decs">处理器</div>

                </div>

            </div>


        </div>

    </div>

    <div class="screen-4">

        <div class="screen-4__wrap">

            <h2 class="screen-4__heading">丰富的手机型号</h2>

            <h3 class="screen-4__subheading">找到适合你的手机</h3>

            <div class="screen-4__type">

                <div class="screen-4__type__item screen-4__type__item_i_1">

                    <div class="screen-4__type__item__color">慕课红</div>

                    <div class="screen-4__type__item__storage">32G/64G/128G</div>

                </div>

                <div class="screen-4__type__item screen-4__type__item_i_2">

                    <div class="screen-4__type__item__color">土豪金</div>

                    <div class="screen-4__type__item__storage">32G/64G/128G</div>

                </div>

                <div class="screen-4__type__item screen-4__type__item_i_3">

                    <div class="screen-4__type__item__color">太空灰</div>

                    <div class="screen-4__type__item__storage">32G/64G/128G</div>

                </div>

                <div class="screen-4__type__item screen-4__type__item_i_4">

                    <div class="screen-4__type__item__color">绅士黑</div>

                    <div class="screen-4__type__item__storage">32G/64G/128G</div>

                </div>

            </div>

        </div>

    </div>

    <div class="screen-5">

        <div class="screen-5__heading">游戏、学习、拍照,有这一部就够了</div>

        <div class="screen-5__subheading">看视频、拍摄高清视频与照片、视频聊天、通话相结合,一机多功能,让您生活更丰富精彩。

        </div>

        <div class="screen-5__bg"></div>

    </div>

    <div class="screen-buy">

        <a href="javascript:;" class="screen-buy__button">立即购买</a>

    </div>

    <footer class="footer">© 2016 imooc.com 京ICP备13046642号</footer>


    <div class="outline">

        <a href="javascript:;" class="outline__item">首页</a>

        <a href="javascript:;" class="outline__item">外观</a>

        <a href="javascript:;" class="outline__item">配置</a>

        <a href="javascript:;" class="outline__item">型号</a>

        <a href="javascript:;" class="outline__item">说明</a>

        <div class="outline__nav-tip"></div>


    </div>

    <script src="js/test.js"></script>

    <script src="js/index.js"></script>

</body>


</html>

/*

BEM 设计模式


模块(没有前缀,多个单词用 - 连接)

元素(元素在模块之后,可以有多个层级,以 __ 连接)

修饰(某元素、或者某模块特别的状态,必须有一个状态名和状态值,使用 _ 链接)


*/


.header {

  background-color#f7f7f7;

}

.header__wrap {

  positionrelative;

  margin0 auto;

  width1200px;

  height80px;

}

.header__logo {

  positionabsolute;

  top50%;

  left20px;

  margin-top-19px;

  width150px;

  height38px;

  font-size20px;

  line-height38px;

  color#07111b;

  text-indent50px;

  backgroundurl("../img/logo.png"no-repeat left center;

  background-size38px 38px;

}

.header__nav {

  positionabsolute;

  right20px;

  top50%;

  margin-top-19px;

  height38px;

}

.header__nav-item{

  color#292f35;

  font-size14px;

  displayblock;

  height38px;

  line-height38px;

  floatleft;

  positionrelative;


  width30px;

  text-aligncenter;

/*  margin: 0;*/

  padding-left40px;

}

.header__nav-item:hover{

  color#f01400;

}

.header__nav-item_status_active{

  color#f01400;

}

.header__nav-item_status_active::after{

/*  content: ' ';

  display: block;

  width: 100%;

  height: 2px;

  position: absolute;

  background-color:#f01400; 

  left: 0;

  bottom: 0;*/

}

.header__nav-item_custom_button{

  background#000;

  color#f4f4f5;

  text-aligncenter;

  width90px;

  border-radius3px;


  /**/

  margin-left:40px;

  padding0;

}

/* 第一屏 */

.screen-1 {

  positionrelative;

  height800px;

  backgroundurl("../img/bg-screen-1.png"center center;

  background-sizecover;

}

.screen-1__heading {

  margin0;

  padding152px 0 0 0;

  font-size46px;

  font-weightnormal;

  text-aligncenter;

  color#4d555d;

}

.screen-1__heading b {

  font-weightnormal;

  color#f01400;

}

.screen-1__phone {

  positionabsolute;

  left50%;

  bottom180px;

  margin-left-687px;

  width1375px;

  height305px;

  backgroundurl("../img/screen-1-phone.png"no-repeat center center;

  z-index2;

}

.screen-1__shadow {

  positionabsolute;

  left50%;

  bottom30px;

  margin-left-616px;

  width1233px;

  height411px;

  backgroundurl("../img/screen-1-shadow.png"no-repeat center center;

  z-index1;

  opacity0.8;

}

/* 第二屏 */

.screen-2 {

  positionrelative;

  height800px;

  background-color#fafafa;

  overflowhidden;

}

.screen-2__heading {

  margin0;

  padding96px 0 0 0;

  font-size46px;

  line-height46px;

  font-weightnormal;

  text-aligncenter;

  color#f01400;

}

.screen-2__subheading {

  margin0;

  padding25px 0 0 0;

  font-size14px;

  line-height28px;

  font-weightnormal;

  text-aligncenter;

  color#2c313c;

}

.screen-2__phone {

  positionabsolute;

  left50%;

  bottom-345px;

  margin-left-464px;

  width928px;

  height873px;

  backgroundurl("../img/screen-2-phone.png"no-repeat center center;

  z-index2;

}

.screen-2__point {

  positionabsolute;

  padding-right112px;

  width108px;

  height22px;

  font-size22px;

  line-height22px;

  color#4d555d;

  backgroundurl("../img/icon-point-right.png"no-repeat center right;

}

.screen-2__point__custom_right {

  padding0 0 0 112px;

  backgroundurl("../img/icon-point-left.png"no-repeat center left;

}

.screen-2__point_i_1 {

  top150px;

  left-164px;

}

.screen-2__point_i_2 {

  top30px;

  right130px;

}

.screen-2__point_i_3 {

  top330px;

  right30px;

}

/* 第三屏 */

.screen-3 {

  positionrelative;

  height800px;

  backgroundurl("../img/bg-screen-3.png"center center;

  background-sizecover;

  overflowhidden;

}

.screen-3__wrap {

  positionrelative;

  margin0 auto;

  width1200px;

  heightauto;

}

.screen-3__heading {

  padding-top94px;

  font-size46px;

  line-height46px;

  text-alignleft;

  color#fff;

}

.screen-3__subheading {

  padding-top25px;

  font-size14px;

  line-height28px;

  text-alignleft;

  color#fff;

}

.screen-3__phone {

  positionabsolute;

  top195px;

  right0;

  bottom-345px;

  width750px;

  height873px;

  backgroundurl("../img/screen-3-phone.png"no-repeat center top;

  z-index2;

}

.screen-3__features {

  positionabsolute;

  top395px;

  left0;

  width324px;

  height280px;

}

.screen-3__features__item {

  floatleft;

  margin0 20px 20px 0;

  width138px;

  height118px;

  text-aligncenter;

  border1px solid #cb7173;

  border-radius3px;

  -webkit-border-radius3px;

  -moz-border-radius3px;

  -ms-border-radius3px;

  -o-border-radius3px;

  color#fff;

}

.screen-3__features__item__number {

  padding28px 0 8px;

  height36px;

  font-size36px;

  line-height36px;

}

.screen-3__features__item__desc {

  height14px;

  font-size14px;

  line-height14px;

}

/* 第四屏 */


.screen-4 {

  positionrelative;

  height800px;

  background-color#fff;

  overflowhidden;

}

.screen-4__wrap {

  positionrelative;

  margin0 auto;

  width1200px;

  heightauto;

}

.screen-4__heading {

  padding-top135px;

  font-size46px;

  line-height46px;

  text-aligncenter;

  color#f01400;

}

.screen-4__subheading {

  padding-top29px;

  font-size14px;

  line-height28px;

  text-aligncenter;

  color#464a4f;

}

.screen-4__type {

  positionabsolute;

  top304px;

  width1260px;

  height270px;

  margin-left40px;

}

.screen-4__type__item {

  width220px;

  margin-right90px;

  height270px;

  floatleft;

  positionrelative;

  text-aligncenter;

  background-sizecover;

}

.screen-4__type__item_i_1 {

  backgroundurl(../img/phone-1.pngno-repeat left top;

}

.screen-4__type__item_i_2 {

  backgroundurl(../img/phone-2.pngno-repeat left top;

}

.screen-4__type__item_i_3 {

  backgroundurl(../img/phone-3.pngno-repeat left top;

}

.screen-4__type__item_i_4 {

  backgroundurl(../img/phone-4.pngno-repeat left top;

}

.screen-4__type__item__color {

  width100%;

  height14px;

  line-height14px;

  font-size14px;

  color#2c3238;

  positionabsolute;

  bottom-44px;

}

.screen-4__type__item__storage {

  width100%;

  height12px;

  line-height12px;

  font-size12px;

  color#a4a9a1;

  positionabsolute;

  bottom-66px;

}


/* 第五屏 */

.screen-5 {

  positionrelative;

  height800px;

  background-color#d9dde1;

  overflowhidden;

}

.screen-5__bg {

  width1920px;

  height433px;

  backgroundurl("../img/bg-screen-5.png"no-repeat center;

  background-sizecontain;

  positionabsolute;

  bottom-100px;

  left50%;

  margin-left-960px;

}

.screen-5__heading {

  padding-top130px;

  font-size46px;

  line-height46px;

  text-aligncenter;

  color#f01400;

}

.screen-5__subheading {

  padding-top29px;

  font-size14px;

  line-height28px;

  text-aligncenter;

  color#464a4f;

}

/* 第六屏 */

.screen-buy {

  positionrelative;

  /* height: 320px; */

  height80px;

  padding120px 0 120px 0;

  background#2b333b url("../img/bg-screen-buy.png"no-repeat center center;

  background-sizecover;

  overflowhidden;

  text-aligncenter;

}

.screen-buy__button {

  displayinline-block;

  height80px;

  width240px;

  font-size24px;

  line-height80px;

  text-aligncenter;

  color#fff;

  background-color#f01414;

  border-radius3px;

  -webkit-border-radius3px;

  -moz-border-radius3px;

  -ms-border-radius3px;

  -o-border-radius3px;

  /* 为hover效果添加一个效果 使得hover不这么生硬 */

  transitionall 0.5s;

  -webkit-transitionall 0.5s;

  -moz-transitionall 0.5s;

  -ms-transitionall 0.5s;

  -o-transitionall 0.5s;

}

.screen-buy__button:hover {

  box-shadow0 0 10px rgba(0000.5);

}

/* 底部 */

.footer {

  height80px;

  text-aligncenter;

  color#fff;

  font-size12px;

  line-height80px;

  background-color#07111b;

}


/* outline css */

.outline {

  positionfixed;

  padding5px 10px;

  bottom120px;

  right0;

  z-index3;

  background-color#fff;

  box-shadow0 4px 12px rgba(0000.5);

}

.outline__item {

  displayblock;

  width40px;

  height30px;

  line-height30px;

  padding5px 0;

  background-color#fff;

  /* margin-top: 5px; */

  border-top1px solid #eee;

  color#939992;

}

.outline__item:first-child {

  bordernone;

}

.outline__item:hover,

.outline__item_status_active {

  color#f01400;

}


/* 导航条样式设置 */

.header {

  transitionall 1s;

  -webkit-transitionall 1s;

  -moz-transitionall 1s;

  -ms-transitionall 1s;

  -o-transitionall 1s;

}

.header_status_back {

  positionfixed;

  top0;

  left0;

  right0;

  backgroundrgba(0000.5);

  z-index3;

}

.header_status_back .header__nav-item,

.header_status_back .header__logo {

  color#fff;

}

.header_status_back .header__nav-item_status_active {

  colorred;

}

.header_status_back .header__nav-item:hover {

  transitionall 1s;

  colorred;

}


/* 大纲出现 */

.outline {

  transformtranslate(100%0);

  transitionall 1s;

}


.outline_status_in {

  transformtranslate(00);

}


.outline__item {

  transitionall 1s;

}

/* 滑动门css */

.header__nav-tip {

  positionabsolute;

  width30px;

  margin-left40px;

  height2px;

  background#f00;

  left0;

  bottom0px;

  transition:  all .5s;

  -webkit-transition:  all .5s;

  -moz-transition:  all .5s;

  -ms-transition:  all .5s;

  -o-transition:  all .5s;

}

.outline__nav-tip{

  positionabsolute;

  width40px;

  margin-left10px;

  height2px;

  background#f00;

  left0;

  bottom0px;

  transition:  all 1s;

  -webkit-transition:  all 1s;

  -moz-transition:  all 1s;

  -ms-transition:  all 1s;

  -o-transition:  all 1s;

}


写回答

1回答

好帮手慕慕子

2020-11-20

同学你好,对于你的问题解答如下:截图中控制台报错的原因是:switchNavItemsActive方法中使用navTip时,还没有获取到navTip元素,无法设置样式,导致报错。

建议:将获取navTip元素的代码调整到前面。

http://img.mukewang.com/climg/5fb72cf309dab1f410870403.jpg

调整后,打开注释的代码,滚动时滑动门效果就可以实现了。如下所示:

http://img.mukewang.com/climg/5fb72d4f096408eb07960415.jpg

祝学习愉快~

0

0 学习 · 40143 问题

查看课程