JavaScript-PC网页特效

PC网页特效

1、元素偏移量offset系列

image-20220303232611020

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
   <style>
      *{
           margin: 0;
           padding: 0;
      }
       .father{
           /*position: relative;*/
           width: 200px;
           height: 200px;
           margin: 100px;
           background-color: purple;
      }
       .son{
           width: 100px;
           height: 100px;
           background-color: beige;
           margin-left: 45px;
           margin-top: 45px;
      }
       .aa{
           position: relative;
           padding: 10px;
           border: 15px green solid;
           width: 100px;
           height: 200px;
           background-color: red;
           margin-top: 200px;
           margin-left: 200px;
      }
   </style>
</head>
<body>
<div class="father">
   <div class="son"></div>
   <div class="aa"></div>
</div>
<script>
   // offset 系列
   let father = document.querySelector('.father');
   let son = document.querySelector('.son');
   let aa = document.querySelector('.aa');
   // 1、可以得到元素的偏移 位置 返回的不带单位的数值
   console.log(father.offsetTop);  //100
   console.log(father.offsetLeft); //100
   // 它以带有定位的父亲为准 如果没有父亲或者父亲没有定位 则以body为准
   console.log(son.offsetTop);     //100
   console.log(son.offsetLeft);    //145
   // 2、可以得到元素的大小 宽度和长度 包含padding + border + width
   console.log(aa.offsetWidth);    //150
   console.log(aa.offsetHeight);   //250
   // 3、返回带有定位的父亲 否则返回的是body
   console.log(son.offsetParent);  //body
   console.log(son.parentNode);    //<div vlass='father'></div>
   //返回父亲,最近一级的亲爸,不管父亲有没有定位                          
</script>
</body>
</html>
2、offset与style的区别

image-20220303235640435

3、获取鼠标在盒子里的坐标
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
   <style>
       div{
           width: 300px;
           height: 300px;
           background-color: purple;
           margin: 200px 200px;
      }
   </style>
</head>
<body>
<div></div>
<script>
   let div = document.querySelector('div');
   div.addEventListener('mousemove',function (e) {
       let x =  e.pageX - div.offsetLeft;
       let y =  e.pageY - div.offsetTop;
       this.innerHTML = 'x坐标为:'+x+' y坐标为:'+y;
  })
</script>
</body>
</html>

image-20220304001612402

4、模态框的拖动
<!DOCTYPE html>
<html>

<head lang="en">
   <meta charset="UTF-8">
   <title></title>
   <style>
       .login-header {
           width: 100%;
           text-align: center;
           height: 30px;
           font-size: 24px;
           line-height: 30px;
      }

       ul,
       li,
       ol,
       dl,
       dt,
       dd,
       div,
       p,
       span,
       h1,
       h2,
       h3,
       h4,
       h5,
       h6,
       a {
           padding: 0px;
           margin: 0px;
      }

       .login {
           display: none;
           width: 512px;
           height: 280px;
           position: fixed;
           border: #ebebeb solid 1px;
           left: 50%;
           top: 50%;
           background: #ffffff;
           box-shadow: 0px 0px 20px #ddd;
           z-index: 9999;
           transform: translate(-50%, -50%);
      }

       .login-title {
           width: 100%;
           margin: 10px 0px 0px 0px;
           text-align: center;
           line-height: 40px;
           height: 40px;
           font-size: 18px;
           position: relative;
           cursor: move;
      }

       .login-input-content {
           margin-top: 20px;
      }

       .login-button {
           width: 50%;
           margin: 30px auto 0px auto;
           line-height: 40px;
           font-size: 14px;
           border: #ebebeb 1px solid;
           text-align: center;
      }

       .login-bg {
           display: none;
           width: 100%;
           height: 100%;
           position: fixed;
           top: 0px;
           left: 0px;
           background: rgba(0, 0, 0, .3);
      }

       a {
           text-decoration: none;
           color: #000000;
      }

       .login-button a {
           display: block;
      }

       .login-input input.list-input {
           float: left;
           line-height: 35px;
           height: 35px;
           width: 350px;
           border: #ebebeb 1px solid;
           text-indent: 5px;
      }

       .login-input {
           overflow: hidden;
           margin: 0px 0px 20px 0px;
      }

       .login-input label {
           float: left;
           width: 90px;
           padding-right: 10px;
           text-align: right;
           line-height: 35px;
           height: 35px;
           font-size: 14px;
      }

       .login-title span {
           position: absolute;
           font-size: 12px;
           right: -20px;
           top: -30px;
           background: #ffffff;
           border: #ebebeb solid 1px;
           width: 40px;
           height: 40px;
           border-radius: 20px;
      }
   </style>
</head>

<body>
<div class="login-header"><a id="link" href="javascript:;">点击,弹出登录框</a></div>
<div id="login" class="login">
   <div id="title" class="login-title">登录会员
       <span><a id="closeBtn" href="javascript:void(0);" class="close-login">关闭</a></span>
   </div>
   <div class="login-input-content">
       <div class="login-input">
           <label>用户名:</label>
           <input type="text" placeholder="请输入用户名" name="info[username]" id="username" class="list-input">
       </div>
       <div class="login-input">
           <label>登录密码:</label>
           <input type="password" placeholder="请输入登录密码" name="info[password]" id="password" class="list-input">
       </div>
   </div>
   <div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登录会员</a></div>
</div>
<!-- 遮盖层 -->
<div id="bg" class="login-bg"></div>
<script>
   // 1. 获取元素
   let as = document.querySelector('#link');
   let closeBtn = document.querySelector('#closeBtn');
   let title = document.querySelector('.login-title');
   let login = document.querySelector('.login');
   let loginbg = document.querySelector('.login-bg');

   // 2. 点击弹出层这个链接 as 让loginbg 和login 显示出来
   as.addEventListener('click',function () {
       loginbg.style.display = 'block';
       login.style.display = 'block';
  })
   // 2. 点击closeBtn 让loginbg 和login 隐藏起来
   closeBtn.addEventListener('click',function () {
       loginbg.style.display = 'none';
       login.style.display = 'none';
  })

   // 4. 开始拖拽
   // (1) 当我们鼠标按下, 就获得鼠标在盒子内的坐标
   title.addEventListener('mousedown',function (e) {
       let x =  e.pageX - login.offsetLeft;
       let y =  e.pageY - login.offsetTop;
       // (2) 鼠标移动的时候,把鼠标在页面中的坐标,减去 鼠标在盒子内的坐标就是模态框的left和top值
       document.addEventListener('mousemove',move)
       function move (e) {
           login.style.left = e.pageX - x + 'px';
           login.style.top = e.pageY - y + 'px';
      }
       // (3) 鼠标弹起,就让鼠标移动事件移除
       document.addEventListener('mouseup',function () {
           document.removeEventListener('mousemove',move);
      })
  })
</script>


</body>

</html>

image-20220304150812310

5、防京东放大镜
window.addEventListener('load',function () {
   let preview_img = document.querySelector('.preview_img');
   let mask = document.querySelector('.mask');
   let big = document.querySelector('.big');
   // 1. 当我们鼠标经过 preview_img 就显示和隐藏 mask 遮挡层 和 big 大盒子
   preview_img.addEventListener('mouseover',function() {
       mask.style.display = 'block';
       big.style.display = 'block';
  })
   preview_img.addEventListener('mouseout',function() {
       mask.style.display = 'none';
       big.style.display = 'none';
  })
   // 2. 鼠标移动的时候,让黄色的盒子跟着鼠标来走
   preview_img.addEventListener('mousemove',function (e) {
       // (1). 先计算出鼠标在盒子内的坐标
       // 鼠标在盒子内的坐标 = 鼠标在页面的坐标 - 盒子距离页面的长度
       let x = e.pageX - this.offsetLeft;
       let y = e.pageY - this.offsetTop;
       // (2) 减去盒子高度 300的一半 是 150 就是我们mask 的最终 left 和top值了
       // (3) 我们mask 移动的距离
       // 鼠标在和遮挡层中间在坐标 = 鼠标在盒子内的坐标 - 遮挡层宽度的一半
       // 能把遮挡层提上去一半
       let maskX = x - mask.offsetWidth / 2;
       let maskY = y - mask.offsetHeight / 2;
       // (4) 如果x 坐标小于了0 就让他停在0 的位置
       // 遮挡层的最大移动距离
       let maskMaxX =  preview_img.offsetWidth - mask.offsetWidth;
       let maskMaxY =  preview_img.offsetHeight - mask.offsetHeight;
       if (maskX <= 0) {
           maskX = 0;
      }else if(maskX >= maskMaxX){
           maskX =  maskMaxX;
      }
       if (maskY <= 0) {
           maskY = 0;
      }else if(maskY >= maskMaxY){
           maskY =  maskMaxY;
      }
       mask.style.left = maskX +'px';
       mask.style.top =  maskY +'px';

       // 3. 大图片的移动距离 = 遮挡层移动距离 * 大图片最大移动距离 / 遮挡层的最大移动距离
       // 大图
       let bigImg = document.querySelector('.bigImg');
       // 大图片最大移动距离
       let bigMax = bigImg.offsetWidth - big.offsetWidth;
       // 大图片的移动距离 X Y
       let bigX = maskX * bigMax / maskMaxX;
       let bigY = maskY * bigMax / maskMaxY;
       bigImg.style.left = -bigX + 'px';
       bigImg.style.top = -bigY + 'px';

  })

})

image-20220304173706748

6、元素可视区client系列

image-20220304180735903

7、立即函数
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
</head>
<body>
   <script>
       //立即执行函数:(function(){})() 或者(function(){}())
       //主要作用:创建一个独立的作用域,避免了命名冲突问题
      (function (a,b) {
           console.log(a+b);
      })(1,9);
      (function(a,b){
           console.log(a-b);
      }(10,7));
   </script>
</body>
</html>
8、pageshow事件
//pageshow 是重新加载页面触发的事件
//pageshow 事件在用户浏览网页时触发。
//pageshow 事件类似于 load 事件,load 事件在页面第一次加载时触发, pageshow 事件在每次加载页面时触发,即 load 事件在页面从浏览器缓存中读取时不触发。
//为了查看页面是直接从服务器上载入还是从缓存中读取,你可以使用 PageTransitionEvent 对象的 persisted 属性来判断。 如果页面从浏览器的缓存中读取该属性返回 ture,否则返回 false
9、元素scroll系列

image-20220304203448826

10、仿淘宝固定侧边栏
<!DOCTYPE html>
<html lang="en">

<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta http-equiv="X-UA-Compatible" content="ie=edge">
   <title>Document</title>
   <style>
       .slider-bar {
           position: absolute;
           left: 50%;
           top: 300px;
           margin-left: 600px;
           width: 45px;
           height: 130px;
           background-color: pink;
      }

       .w {
           width: 1200px;
           margin: 10px auto;
      }

       .header {
           height: 150px;
           background-color: purple;
      }

       .banner {
           height: 250px;
           background-color: skyblue;
      }

       .main {
           height: 1000px;
           background-color: yellowgreen;
      }

       span {
           display: none;
           position: absolute;
           bottom: 0;
      }
   </style>
</head>

<body>
<div class="slider-bar">
   <span class="goBack">返回顶部</span>
</div>
<div class="header w">头部区域</div>
<div class="banner w">banner区域</div>
<div class="main w">主体部分</div>
<script>
   //1. 获取元素
   let sliderbar = document.querySelector('.slider-bar');
   let banner = document.querySelector('.banner');
   // 获取main 主体元素
   let main = document.querySelector('.main');
   let goBack = document.querySelector('.goBack');
   let mainTop = main.offsetTop;
   // banner.offestTop 就是被卷去头部的大小 一定要写到滚动的外面
   let bannerTop = banner.offsetTop;
   // 当我们侧边栏固定定位之后应该变化的数值
   let sliderbarTop = sliderbar.offsetTop - bannerTop;
   // 2. 页面滚动事件 scroll
   document.addEventListener('scroll',function () {
       // window.pageYOffset 页面被卷去的头部
       if (window.pageYOffset >= bannerTop){
           // 3 .当我们页面被卷去的头部大于等于了 bannerTop 此时 侧边栏就要改为固定定位
           sliderbar.style.position = 'fixed';
           sliderbar.style.top = sliderbarTop +'px';
      }else {
           sliderbar.style.position = 'absolute';
           sliderbar.style.top = '300px';
      }
       // 4. 当我们页面滚动到main盒子,就显示 goback模块
       if (window.pageYOffset >= mainTop){
           goBack.style.display = 'block';
      }else {
           goBack.style.display = 'none';
      }
  })
</script>
</body>
</html>

image-20220304213518579

11、三大系列总结

image-20220304213615952

image-20220304214228320

12、mouseenter和mouseover的区别

image-20220304214541487

13、动画实现原理
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
   <style>
       div{
           top:40px;
           position: absolute;
           width: 100px;
           height: 100px;
           background-color: purple;
      }
       span{
           position: absolute;
           top: 200px;
           width: 50px;
           height: 50px;
           background-color: red;
      }
   </style>
</head>
<body>
<div></div>
<span></span>
<button>点我小红就跑</button>
<script>
//     核心原理:通过定时器setInterval()不断移动盒子位置
//     实现步骤:
//     1、获取盒子当前位置
//     2、让盒子在当前位置加上1个移动距离
//     3、利用定时器不断重复这个操作
//     4、加一个结束定时器的条件
//     5、注意此元素需要添加定位,才能使用element.style.left

   let div = document.querySelector('div');
   let span = document.querySelector('span');
   let btn = document.querySelector('button');
//     当我们不断的点击按钮,这个元素的速度会越来越快,因为开启了太多的定时器
//     解决方案就是 让我们元素只有一个定时器执行
//     先清除以前的定时器,只保留当前的一个定时器执行
   function animate(object,target,speed){
       clearInterval(object.timer);
       object.timer = setInterval(function () {
           if (object.offsetLeft >= target){
               // 停止动画 本质是停止定时器
               clearInterval(object.timer);
          }
           object.style.left = object.offsetLeft + 1 + 'px'
      },speed)
  }
   btn.addEventListener('click',function () {
       animate(span,400,10);
  })
   animate(div,500,30);

</script>
</body>
</html>
14、缓动动画
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
   <style>
       span{
           position: absolute;
           top: 200px;
           width: 50px;
           height: 50px;
           background-color: red;
      }
   </style>
</head>
<body>
<span></span>
<button>点我小红就跑</button>
<script>
   // 缓动动画函数封装obj目标对象 target 目标位置
   // 思路:
   // 1. 让盒子每次移动的距离慢慢变小, 速度就会慢慢落下来。
   // 2. 核心算法:(目标值 - 现在的位置) / 10 做为每次移动的距离 步长
   // 3. 停止的条件是: 让当前盒子位置等于目标位置就停止定时器
   let span = document.querySelector('span');
   let btn = document.querySelector('button');
   function animate(object,target){
       clearInterval(object.timer);
       object.timer = setInterval(function () {
           let speed = (target - object.offsetLeft) /10
           if (object.offsetLeft === target){
               // 停止动画 本质是停止定时器
               clearInterval(object.timer);
          }
           object.style.left = object.offsetLeft + speed + 'px'
      },8)
  }
   btn.addEventListener('click',function () {
       animate(span,400);
  })
   animate(div,500);

</script>
</body>
</html>
15、仿淘宝返回顶部
<!DOCTYPE html>
<html lang="en">

<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta http-equiv="X-UA-Compatible" content="ie=edge">
   <title>Document</title>
   <style>
       .slider-bar {
           position: absolute;
           left: 50%;
           top: 300px;
           margin-left: 600px;
           width: 45px;
           height: 130px;
           background-color: pink;
      }

       .w {
           width: 1200px;
           margin: 10px auto;
      }

       .header {
           height: 150px;
           background-color: purple;
      }

       .banner {
           height: 250px;
           background-color: skyblue;
      }

       .main {
           height: 1000px;
           background-color: yellowgreen;
      }

       span {
           display: none;
           position: absolute;
           bottom: 0;
      }
   </style>
</head>

<body>
<div class="slider-bar">
   <span class="goBack">返回顶部</span>
</div>
<div class="header w">头部区域</div>
<div class="banner w">banner区域</div>
<div class="main w">主体部分</div>
<script>
   //1. 获取元素
   let sliderbar = document.querySelector('.slider-bar');
   let banner = document.querySelector('.banner');
   // 获取main 主体元素
   let main = document.querySelector('.main');
   let goBack = document.querySelector('.goBack');
   let mainTop = main.offsetTop;
   // banner.offestTop 就是被卷去头部的大小 一定要写到滚动的外面
   let bannerTop = banner.offsetTop;
   // 当我们侧边栏固定定位之后应该变化的数值
   let sliderbarTop = sliderbar.offsetTop - bannerTop;
   // 2. 页面滚动事件 scroll
   document.addEventListener('scroll',function () {
       // window.pageYOffset 页面被卷去的头部
       if (window.pageYOffset >= bannerTop){
           // 3 .当我们页面被卷去的头部大于等于了 bannerTop 此时 侧边栏就要改为固定定位
           sliderbar.style.position = 'fixed';
           sliderbar.style.top = sliderbarTop +'px';
      }else {
           sliderbar.style.position = 'absolute';
           sliderbar.style.top = '300px';
      }
       // 4. 当我们页面滚动到main盒子,就显示 goback模块
       if (window.pageYOffset >= mainTop){
           goBack.style.display = 'block';
      }else {
           goBack.style.display = 'none';
      }
  })

   //返回顶部
   goBack.addEventListener('click',function () {
       animate(window,0);
  })
   function animate(object,target){
       clearInterval(object.timer);
       object.timer = setInterval(function () {
           let speed = (target - window.pageYOffset) /10
           //把步长改为整数,去掉小数
           speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
           if (window.pageYOffset === target){
               // 停止动画 本质是停止定时器
               clearInterval(object.timer);
          }
           // 跳转       x   y
           window.scroll(0,window.pageYOffset + speed);
      },8)
  }


</script>
</body>
</html>
16、筋斗云导航栏
<!DOCTYPE html>
<html>

<head lang="en">
   <meta charset="UTF-8">
   <title></title>
   <style>
      * {
           margin: 0;
           padding: 0
      }
       
       ul {
           list-style: none;
      }
       
       body {
           background-color: black;
      }
       
       .c-nav {
           width: 900px;
           height: 42px;
           background: #fff url(images/rss.png) no-repeat right center;
           margin: 100px auto;
           border-radius: 5px;
           position: relative;
      }
       
       .c-nav ul {
           position: absolute;
      }
       
       .c-nav li {
           float: left;
           width: 83px;
           text-align: center;
           line-height: 42px;
      }
       
       .c-nav li a {
           color: #333;
           text-decoration: none;
           display: inline-block;
           height: 42px;
      }
       
       .c-nav li a:hover {
           color: white;
      }
       
       .c-nav li.current a {
           color: #0dff1d;
      }
       
       .cloud {
           position: absolute;
           left: 0;
           top: 0;
           width: 83px;
           height: 42px;
           background: url(images/cloud.gif) no-repeat;
      }
   </style>
   <script src="animate.js"></script>
</head>

<body>
   <div id="c_nav" class="c-nav">
       <span class="cloud"></span>
       <ul>
           <li class="current"><a href="#">首页新闻</a></li>
           <li><a href="#">师资力量</a></li>
           <li><a href="#">活动策划</a></li>
           <li><a href="#">企业文化</a></li>
           <li><a href="#">招聘信息</a></li>
           <li><a href="#">公司简介</a></li>
           <li><a href="#">我是佩奇</a></li>
           <li><a href="#">啥是佩奇</a></li>
       </ul>
   </div>
</body>
<script>
   // 1、获取元素
   let c_nav = document.querySelector('.c-nav') ;
   let cloud = document.querySelector('.cloud');
   let lsi = c_nav.querySelectorAll('li');
   // 做为筋斗云的起始位置
   let current = 0;
   // 2、给所有的li绑定事件
   for (let i = 0; i < lsi.length; i++ ){
       // (1) 鼠标经过,把当前小li的位置做为目标值
       lsi[i].addEventListener('mouseenter',function () {
           animate(cloud,this.offsetLeft);
      });
       // (2) 鼠标离开就回到起始位置
       lsi[i].addEventListener('mouseleave',function () {
           animate(cloud,current);
      })
       // (3) 当我们鼠标点击,就把当前位置做为目标值
       lsi[i].addEventListener('click',function () {
           current = this.offsetLeft;
      })
  }

</script>

</html>
posted @ 2022-03-06 15:05  小鹅爸爸  阅读(39)  评论(0编辑  收藏  举报