锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

JavaScript 学习 Day6_p287-p334

时间:2022-09-07 04:00:00 二极管atv02w220b

在这里插入图片描述

DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>Documenttitle>     <style> *{ 
          margin: 0; padding: 0; } .father{ 
          width: 200px; height: 200px; background-color: pink; margin: 150px; position: relative; } .son{ 
          width: 100px; height: 100px; background-color: yellow; margin-left: 45px; } .w{ 
          width: 200px; height: 200px; background-color: skyblue; margin: 0 auto 200px; padding: 10px; border: 15px solid red; } style>
head>
<body>
    <div class="father">
        <div class="son">div>
    div>

    <div class="w">div>
    <script> //offset系列 var father = document.querySelector('.father'); var son = document.querySelector('.son'); //1.可以得到元素的偏移 位置 返回的不带单位的数值 console.log(father.offsetTop); console.log(father.offsetLeft); //它以带有定位的父亲为准 如果没有父亲 或者父亲没有定位 则以body为准 console.log(son.offsetTop); console.log(son.offsetLeft); //2.可以得到元素的大小 宽度和高度 是包含padding + border + width var w = document.querySelector('.w'); console.log(w.offsetWidth); console.log(w.offsetHeight); //3.返回带有定位的父亲 否则返回的是body console.log(son.offsetParent);//返回带有定位的父亲 否则一级一级往上找 知道找到带有定位的父亲为止 console.log(son.parentNode);//返回父亲 是最近一级的父亲 亲爸爸 不管有没有定位 script>
body>
html>

获取鼠标在盒子内的坐标案例

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>获取鼠标在盒子内的坐标title>
    <style> .box{ 
           width: 200px; height: 200px; background-color: pink; margin: 0 auto; } style>
head>
<body>
    <div class="box">
        <div class="son">div>
    div>

    <script> var box = document.querySelector('.box'); box.addEventListener('mousemove', function(e){ 
           console.log('x坐标为:' + (e.pageX - box.offsetLeft)); console.log('y坐标为:' + (e.pageY - box.offsetTop)); }); script>
body>
html>

很重要的模态框案例

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.获取元素 var login = document.querySelector('.login'); var mask = document.querySelector('.login-bg'); var link = document.querySelector('#link'); var closeBtn = document.querySelector('#closeBtn'); //鼠标只在title的范围内才能拖拽 var title = document.querySelector('#title'); //2.点击弹出层这个链接 link 让mask 和login 显示出来 link.addEventListener('click', function(){ 
           mask.style.display = 'block'; login.style.display = 'block'; }); //3.点击关闭mask和login closeBtn.addEventListener('click', function(){ 
           mask.style.display = 'none'; login.style.display = 'none'; }); //4.开始拖拽 //(1)当我们鼠标按下就获得鼠标在盒子内的坐标 title.addEventListener('mousedown', function(e){ 
           var x = e.pageX - login.offsetLeft; var 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(e){ 
           document.removeEventListener('mousemove', move); }); }); script>
body>

html>


立即执行函数的用法

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>立即执行函数title>
head>
<body>
    <script> // 1.立即执行函数:不需要调用,立马能够自己执行的函数 function fn(){ 
           console.log(1); } fn(); //2.写法 也可以传递参数进来 // (function(){})(); 或者 (function(){}()); (function(a, b){ 
           console.log(a + b); var num = 10;//局部变量 })(1, 2);//第二个小括号可以看看做调用函数 (function sum(a, b){ 
           console.log(a + b); var num = 10;//局部变量 }(2, 3)); //3.立即执行函数最大的作用就是独立创建了一个作用域,里面所有的变量都是局部变量不会有命名冲突的情况 script>
body>
html>

pageshow事件可以解决火狐浏览器的页面刷新bug

resize事件表示页面大小发生变化时触发

flexible分析

(function flexible(window, document) { 
        
    // 获取的html 的根元素
    var docEl = document.documentElement
        // dpr 物理像素比
    var dpr = window.devicePixelRatio || 1

    // adjust body font size 设置我们body 的字体大小
    function setBodyFontSize() { 
        
        // 如果页面中有body 这个元素 就设置body的字体大小
        if (document.body) { 
        
            document.body.style.fontSize = (12 * dpr) + 'px'
        } else { 
        
            // 如果页面中没有body 这个元素,则等着 我们页面主要的DOM元素加载完毕再去设置body
            // 的字体大小
            document.addEventListener('DOMContentLoaded', setBodyFontSize)
        }
    }
    setBodyFontSize();

    // set 1rem = viewWidth / 10 设置我们html 元素的文字大小
    function setRemUnit() { 
        
        var rem = docEl.clientWidth / 10
        docEl.style.fontSize = rem + 'px'
    }

    setRemUnit 

相关文章