onscroll --滑动效果和高亮当前元素

2018-03-11  本文已影响0人  CeaCrab

滑动变化效果
onscroll --滑动API
它的 onscrollX,onscrollY两个值,分别表示XY滑动后的距离。

// js只负责通过元素中的id来操控元素的class达到效果
window.onscroll = function(xxx){
  if(window.scrollY > 0){
    id.calssList.add('stick')
  }else{
    id.classList.remove('stick')
  }
console.log(window.scrollY) //打印Y坐标值
}
        window.onscroll = function(xxx){
            if(window.scrollY > 0){
            id.calssList.add('stick')
            }else{
                id.classList.remove('stick')
            }
            console.log('window.scrollY') //‘打印滚动Y坐标值’
            console.log(window.scrollY) //滚动Y坐标值为
            let 标记元素 = document.querySelectoeAll('[data-x]') // 通过选择器获取所有标记属性的元素
            for( let i = 0; 标记.length; i++){ //遍历这些元素
                console.log('标记[i].offsetTop') 
                console.log(标记[i].offsetTop) // 标记元素top值,不会随滚动高度而变化
            }
        }
    
可以计算175/947/1321哪个距离滚动坐标12最近,确定应该是哪个元素
        window.onscroll = function(xxx){
            if(window.scrollY > 0){
            id.calssList.add('stick')
            }else{
                id.classList.remove('stick')
            }
            console.log('window.scrollY') //‘打印滚动Y坐标值’
            console.log(window.scrollY) //滚动Y坐标值为
            let 标记元素 = document.querySelectoeAll('[data-x]') // 通过选择器获取所有标记属性的元素
            let minindex = 0 //假设最小值是第0个
//循环找最小值
            for( let i = 1; 标记.length; i++){ //遍历这些元素 i就从第1个开始
                if(Math.abs(标记元素[i].offsetTop-window.scrollTop() < Math.abs(标记元素[minindex].offsetTop-window.scrollTop)){
                        minIndex = i
                }
            }
            console.log(minIndex)
//循环添加active
            //首先消掉active,然后再加上
            for( let i = 0; 标记.length; i++){
            标记元素[minIndex].classList.remove('active')

            }
            for( let i = 0; 标记.length; i++){
            标记元素[minIndex].classList.add('active')

            }
        }
css 0 1
2
//找到a标签
       let id = 标记元素[minIndex].id 
            
            console.log(id)
            let a = document.querySelector('a[href="#' + id +  '"]') 
            //通过id获取a标签 --字符串加变量(id)加字符串
        }
id
            let 标记元素 = document.querySelectoeAll('[data-x]') // 通过选择器获取所有标记属性的元素
            let minindex = 0 //假设最小值是第0个
            for( let i = 1; 标记.length; i++){ //遍历这些元素 i就从第1个开始
                if(Math.abs(标记元素[i].offsetTop-window.scrollTop() < Math.abs(标记元素[minindex].offsetTop-window.scrollTop)){
                        minIndex = i
                }
            }
            console.log(minIndex)
            //首先消掉active,然后再加上
            for( let i = 0; 标记.length; i++){
            标记元素[minIndex].classList.remove('active')

            }
            for( let i = 0; 标记.length; i++){
            标记元素[minIndex].classList.add('active')
            }
//minIndex 距离窗口最近的标记元素
            let id = 标记元素[minIndex].id 
            
            console.log(id)
            let a = document.querySelector('a[href=#'+ id + ']') //通过id获取a标签
            let li = a.praentNode //获取a父级元素li标签
            let  brothersAndMe= li.parentNode.children //所有li标签的儿子们,包括自己
            fo(let i = 0; i<brothersAndMe.length; i++){
//ligntheight' -- 可以给状态名变个名字,效果还是active的效果,css下公用一个状态效果
                brothersAndMe[i].classList.remove('ligntheight') 
            }
            li.classList.add('active')
//JS代码
//给所有标记元素添加相同的类
let 标记元素 = document.querySelectoeAll('[data-x]') 
    for( let i = 0; 标记.length; i++){
        标记元素[i]classList.add('offset') //offset脱离初始位置的类名        
    }
}
*************************************************************************************
            let 标记元素 = document.querySelectoeAll('[data-x]') // 通过选择器获取所有标记属性的元素
            let minindex = 0 //假设最小值是第0个
            for( let i = 1; 标记.length; i++){ //遍历这些元素 i就从第1个开始
                if(Math.abs(标记元素[i].offsetTop-window.scrollTop() < Math.abs(标记元素[minindex].offsetTop-window.scrollTop)){
                        minIndex = i
                }
            }
            console.log(minIndex)
            //首先消掉active,然后再加上
            for( let i = 0; 标记.length; i++){
            标记元素[minIndex].classList.remove('active')

            }
            for( let i = 0; 标记.length; i++){
            标记元素[minIndex].classList.add('active')
            }

            //minIndex 距离窗口最近的标记元素 给它添加一个class,css添加[data-x].animate{添加动画}
            标记元素[minIndex].classList.remove('offset') 
            let id = 标记元素[minIndex].id 
            
            console.log(id)
            let a = document.querySelector('a[href=#'+ id + ']') //通过id获取a标签
            let li = a.praentNode //获取a父级元素li标签
            let  brothersAndMe= li.parentNode.children //所有li标签的儿子们,包括自己
            fo(let i = 0; i<brothersAndMe.length; i++){
                rothersAndMe[i].classList.remove('换的状态名') 
            }
            li.classList.add('active')
            
        }
*********************************************************
//css代码
//脱离初始位置
[data-x].offset{
    transform: translateY(60px);
}
[data-x]{
    transform: translateY(0);
    transition: all 0.5s;
}
//初始位置动画效果
@keyframes zzz{
   0%{
     transform: translateY(-60px);
   }
   100%{
     transform: translateY(0);
   }
}

[data-x].animate{
    animation: zzz 0.5s;
 } 

section.skill .progressBar{
    height: 5px;
    border-radius: 2px;
    background-color: #FAE1E1;
    margin: 7px 0;
    overflow: hidden; //父元素中默认隐藏
}
section.skill .progressBar .progress{
    height: 100%;
    background: #E6686A;
    width: 50%;
    border-radius: 2px;
    transform: translateX(0);
    transition: all 1s;
}
section.skill.offset .progress{
    transform: translateX(-100%);  //改变坐标
}
深度截图_选择区域_20180311011443.png
上一篇 下一篇

猜你喜欢

热点阅读