CSS与JS

目录

一、定位属性

2.绝对定位

3.固定定位

二、盒子模型

三、扩展属性与网页设计

1.扩展属性

2.网页制作

四、JS概述

1.概述

2.组成部分

3.搭环境

五、JS基础语法

1.变量与数据类型

2.对象与数组

3.运算符

4.字符串操作

六、函数

1.函数应用

2.弹窗函数

3.系统提供的函数

七、事件


一、定位属性

描述:定位就是指定控件的位置,有三种定位方式:相对定位,绝对定位,固定定位

1.相对定位

相对定位:相对于当前位置的定位,移动位置后,原有位置保留

如:

<head>
    <style type="text/css">
        div{
            width: 150px;
            height: 150px;
        }
        #one{
            background-color: red;
        }
        #two{
            background-color: green;
            position: relative; /* 相对定位 */
            left: 50px;  /*左侧间隔50像素 */
            top: 30px;   /* 顶部间隔30像素 */
        }
        #threw{
            background-color: blue;
        }
    </style>
</head>
<body>
    <div id="one"></div>
    <div id="two"></div>
    <div id="threw"></div>
</body>

2.绝对定位

绝对定位:相对于父容器的定位,如果父容器有定位,则基于父容器的定位;如果没有,则最终相对于body定位; 不会保留原有位置

<head>
    <style type="text/css">
        .dd{
            width: 150px;
            height: 150px;
        }
        .one{
            background-color: yellow;
        }
        .two{
            background-color: green;
            position: absolute; /* 绝对定位:基于父容器定位,且不会保留原有位置 */
            left: 50px;  /*左侧间隔50像素 */
            top: 30px;   /* 顶部间隔30像素 */
        }
        .threw{
            background-color: blue;
        }
        #parent{
            outline: dashed 2px gray;
            position: relative; /* 父容器的定位 */
        }
    </style>
</head>
<body>
    <div id="parent">
        <div class="one dd"></div>
        <div class="two dd"></div>
        <div class="threw dd"></div>
    </div>

</body>

3.固定定位

固定定位:和绝对定位类似,都是基于父容器定位;唯一的区别是,固定定位的位置完全固定

<head>
    <style type="text/css">
        .dd{
            width: 150px;
            height: 150px;
        }
        .one{
            background-color: yellow;
            position: fixed; /* 固定定位也是基于父容器定位,只是固定住了,不能移动 */
            left: 0px; /*相对body的left为0 */
            bottom: 50%;
        }
        .two{
            background-color: green;
            position: fixed;
            left: 0px; /*相对body的left为0 */
            bottom: 0;
        }
        .threw{
            background-color: blue;
            position: fixed;
            right: 0px; /*相对body的left为0 */
            bottom: 0px;
        }
    </style>
</head>
<body>
    <div class="one dd"></div>
    <div class="two dd"></div>
    <div class="threw dd"></div>
    <br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br />
</body>

二、盒子模型

盒子模型,主要讲解边框,内边距,外边距的设计

边框:类似于outline轮廓,都是将控件内容的包裹;(比轮廓更常用)

内边距:在边框与控件内容之间的间距,默认0px

外边距:边框的外面,控件与控件之间的间距,默认0px

<head>
    <style type="text/css">
        .dd{
            width: 100px;
            height: 100px;
        }
        .one{
            border-color: red;  /* 边框颜色 */
            border-width: 4px;  /* 线条宽度 */
            border-style: solid; /* 边框样式-实线 */
            padding-left: 30px; /*左侧内边距,会撑大容器 */
            /*margin-bottom: 50px;*/ /* 底部外边距:和第二个控件间距50px */
        }
        .two{
            border: dashed 4px green; /*边框组合,和轮廓属性类似 */
            margin-top: 50px; /* 顶部外边距:和第一个控件间距50px */
        }
    </style>
</head>
<body>
    <div class="one dd"></div>
    <div class="two dd"></div>
</body>

三、扩展属性与网页设计

1.扩展属性

<head>
    <style type="text/css">
        div{
            width: 250px;
            line-height: 80px;
            background-color: red;
            font-size: 40px;
            color: white;
            text-align: center;
            border-radius: 10px; /*圆角弧度 */
            text-shadow: 20px 10px 10px blue;
            /* 盒子阴影,和文本阴影类似 */
            box-shadow: 10px 15px 15px gray;
        }
        body{
            /* 设置多张图片: 图片和位置使用,拼接 */
            background-image: url(../img/002.png),url(../img/004.png);
            background-repeat: no-repeat;
            background-size: 300px 400px; /* 值为宽和高的值 */
            background-position: 100px 100px,400px 100px; /* x和y轴坐标点位置 */
        }
    </style>
</head>
<body>
    <div>扩展属性</div>
</body>

2.网页制作

<head>
    <style type="text/css">
        #left{
            float: left;
            line-height: 32px;
        }
        #right{
            float: right;	
            line-height: 32px;
        }
        #parent{
            width: 100%;
            height: 32px;
            background-color: gainsboro;
            overflow: hidden; /*清除浮动 */
        }
    </style>
</head>
<body>
    <div id="parent">
        <div id="left"><span style="color: red;">亲,请登录&nbsp;&nbsp;</span>免费注册&nbsp;&nbsp;手机逛淘宝</div>
        <div id="right"><span style="color: red;">淘宝网首页&nbsp;&nbsp;</span>我的淘宝&nbsp;&nbsp;购物车&nbsp;&nbsp;收藏夹</div>
    </div>
</body>

四、JS概述

1.概述

概述:解释型的脚本语言,就是依附在浏览器中,用于解释HTML的触发机制;例如,点击控件触发

如果把HTML看成房子结构,CSS看成装修效果,那么JS相当于智能家居

JS也是一种弱类型语言(java是强类型),在定义时都是var类型

2.组成部分

JS中需要学习三部分:

1.ECMASCript:统一脚本语言的标准(基础语法)

2.DOM-文档对象模型,(语法结合HTML使用)

3.BOM-浏览器对象模型,(JS与浏览器的交互)

3.搭环境

有两种脚本方式:

1.直接写脚本标签:<script></script>

2.引入JS文件

<!-- 方式1.直接引入js文件 -->
<script src="../js/hello.js"></script><br />
<!-- 方式2:script标签 -->
<!--		<script>
	alert("hello,world"); //警告提示
</script>-->

五、JS基础语法

1.变量与数据类型

/* 数据类型:表示值的数据类型,定义时都时var类型
			   string,number,boolean,undefined,object 
			 * */
var a = 1;
document.write(a+"<br />");  //页面打印
document.write(typeof a+"<br />");  //number
a = "abc"; 
document.write(a+"<br />");  //页面打印
document.write(typeof a+"<br />");  //string
a = true; 
document.write(a+"<br />");  //页面打印
document.write(typeof a+"<br />");  //boolean

var b;
document.write(b+"<br />");  //页面打印
document.write(typeof b+"<br />");  //undefined

document.write((b+1)+"<br />"); //undefined+1=NaN
document.write(typeof (b+1)+"<br />");  //number

b=3.14;
document.write(b+"<br />");  //页面打印
document.write(typeof b+"<br />");  //undefined

b = null;
document.write(b+"<br />");  //页面打印
document.write(typeof b+"<br />");  //object

2.对象与数组

/* 通过json格式创建对象   格式:{key:value,key2:value2}
			 * */
var student = {id:1001,name:"zs",age:30};
document.write(student.id+"--"+student.name+"<br />");

//数组:
var a = [1,3,5]; //类似java的静态赋值
document.write(a+"<br />");
//数组的增删改查:
a[3] = 8;  //添加
document.write(a+"<br />");
a[3] = 9;  //修改
document.write(a+"<br />");
a[5] = 66;
document.write("------遍历(查找)-------<br />");
for(var i=0;i<a.length;i++){ //和java类似进行遍历和下标操作
    document.write(a[i]+"<br />");
}
delete a[3]; //删除单个元素
document.write(a+"<br />");
document.write("------删除所有-------<br />");
a.length = 0; //删除所有
document.write(a+"<br />");

var arr = new Array(); //动态赋值
arr.push(66,22,88); //存多个元素
document.write(arr+"<br />");

3.运算符

//-------关系运算符-------
var a = 1;
var b = 2;
document.write((a==b)+"<br />"); //false
document.write((a==a)+"<br />"); //true

//== vs  ===
//==:只比较内容   ===:比较内容和类型
document.write((a=="1")+"<br />"); //true
document.write((a==="1")+"<br />"); //false
document.write((a===1)+"<br />"); //true

//--------算数运算符---------
document.write((5/2)+"<br />"); //2.5
//---数字字符串运算符:+是拼接符;其余的会转成number再计算 ---
document.write(("5"+2)+"<br />"); //52 字符串拼接符号
document.write(("5"-2)+"<br />"); //3 

//if分支语句判断,除了6个值为false,其余都为true
if(0){ //false 0 ""   null  undefined NaN 
    document.write("为true<br />");
}else{
    document.write("为false<br />");
}

4.字符串操作

var a = "hello";
document.write(a.concat("666"));  //字符串拼接
document.write(a.startsWith("he"));  //以he开头
document.write(a.toUpperCase()); //转大写
document.write("  aaa  ".trim()); //去除左右空格
document.write("  a,a,a  ".split(",")); //拆分

六、函数

S中的函数和Java中的方法类似,都是用于封装;都有调用和具体实现两部分

1.函数应用

//分类:无参   带参   带返回值
//1.无参数无返回值函数
test();  //函数调用
function test(){ //函数实现
    console.log("打印到控制台--无参无返回值");
}

//2.带参数带返回值函数
var max = getMax(3,5);
console.log(max);

function getMax(a,b){
    return (a>b)?a:b;
}

//问题:JS中是否有重载函数?  案例:求两个输的和,三个输的和,四个输的和
//结论:JS中没有重载,同名方法,后面的会覆盖前面的
//如果需要完成重载的调用,则需要借助 arguments(类似java的可变长参数)
console.log(add(1,2)); //3 
console.log(add(1,2,3));  //6
console.log(add(1,2,3,4)); //10

/*
			function add(a,b){
				return a+b;
			}
			function add(a,b,c){
				return a+b+c;
			}
			function add(a,b,c,d){
				return a+b+c+d;
			}*/
function add(){
    //arguments:专门接收参数的数组
    var sum = 0;
    for(var i=0;i<arguments.length;i++){
        sum += arguments[i];
    }
    return sum; //如果不写return,则接收undefined
}
//扩展:函数实现的另一中写法:把函数名当成变量的写法
aaa = function(){ //注意:这种写法,函数体必须放在函数调用的前面
    console.log("另一种函数实现");
}
aaa();  //函数调用

2.弹窗函数

//1.警告框  alert
//alert("弹出");

//2.确认框   confirm
/*
			if(confirm("你确认接收这个惊喜吗")){
				console.log("你点击了确认");
			}else{
				console.log("你点击了取消");
			}*/

//3.提示框   prompt 点击确认,返回内容   取消,则返回null  
//参数1:提示信息   参数2:默认填充的内容
var a = prompt("欢迎领导来千锋参观!","凤姐"); 
console.log(a);

3.系统提供的函数

//parseInt: 数字字符串转整数
var a = parseInt("3.14");
console.log(a);  //3
console.log(typeof a);

//parseFloat: 数字字符串转小数
a = parseFloat("3.14");
console.log(a);  //3.14
console.log(typeof a);

//isNaN:判断是否为非数字,是非数字则true;不是非数字则false
console.log(isNaN(NaN));  //true
console.log(isNaN(3));    //false
console.log(isNaN("3"));  //false
console.log(isNaN("abc"));  //true

七、事件

述:事件是HTML和JS之间进行数据交互的桥梁

双方的交互需要函数来完成,在HTML中编写函数调用,JS中进行函数的实现

<head>
    <script>
        function change(){
            alert("change事件..");
        }
        function myclick(){
            alert("click事件");
        }
        function myover(){
            alert("鼠标移入事件");
        }
        function myout(){
            alert("鼠标移出事件");
        }
        function myup(){
            console.log("键盘弹起");
        }
        function mydown(){
            console.log("键盘按下");
        }
        function mysub(){
            alert("提交事件");
        }

        //onload:html的标签都加载完成后,系统会自动调用onload方法
        onload=function(){
            alert("页面加载完成后的触发");
        }
    </script>
</head>
<body>
    <select onchange="change()">
        <option>湖南</option>
        <option>湖北</option>
        <option>广东</option>
    </select><br />
    <h2 onclick="myclick()">二级标题</h2>
    <input type="button" value="鼠标移入" onmouseover="myover()" />
    <input type="button" value="鼠标移出" onmouseout="myout()" />
    <br />
    <input type="text" onkeyup="myup()" onkeydown="mydown()" /><br />
    <form onsubmit="mysub()">
        <input type="submit" value="提交" />
    </form>
</body>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值