JavaScript 8—读取样式

操作内联样式

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        #box1 {
            width: 200px;
            height: 200px;
            background-color: red;
        }
    </style>

    <script type="text/javascript">
        window.onload = function () {
            /*
                点击按钮以后,修改box1的大小
            */
            //获取box1
            var box1 = document.getElementById("box1");
            //为按钮绑定一个单击响应函数
            var btn01 = document.getElementById("btn01");
            btn01.onclick = function () {
                //修改box1的宽度
                /*
                    通过JS修改元素的样式
                        语法:元素.style.样式名 = 样式值

                    注意:如果css的样式中含有“-”
                        这种名称在JS中是不合法的,比如background-color
                        需要将这种样式名修改为驼峰命名法
                        去掉“-”,然后将“-”后的字母大写

                    我们通过style属性设置的样式都是内联样式,
                        而内联样式有较高的优先级,所以通过JS修改的样式往往会立即显示

                    但是如果在样式中写了“!improtant”,则此时样式会有最高的优先级
                        即使通过JS也不能覆盖该样式,此时将会导致JS修改的样式失效
                        所以尽量不要为样式添加“!improtant”
                */
                box1.style.width = "300px";
                box1.style.height = "300px";
                box1.style.backgroundColor = "yellow";
            }


            //点击按钮2以后,读取元素的样式
            var btn02=document.getElementById("btn02");
            btn02.onclick=function(){
                //读取box1的样式
                /*
                    语法:
                        元素.style.样式名

                    通过style属性设置和读取的都是内联样式
                     无法读取样式表中的样式
                */
                alert(box1.style.backgroundColor);
            }
        }
    </script>
</head>

<body>
    <button id="btn01">点我一下</button>
    <button id="btn02">点我一下</button>
    <br /><br />
    <div id="box1"></div>
</body>

</html>

 获取元素的样式

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        #box1 {
            width: 100px;
            height: 100px;
            background-color: yellow;
        }
    </style>

    <script type="text/javascript">
        window.onload = function () {
            //点击按钮以后读取box1的样式
            var box1 = document.getElementById("box1");
            var btn01 = document.getElementById("btn01");
            btn01.onclick = function () {
                //读取box1的宽度
                //alert(box1.style.width);
                /*
                    获取元素的当前显示的样式
                     语法:元素.currentStyle.样式名
                    它可以用来读取当前元素正在显示的样式
                    如果当前元素没有设置该样式,则获取它的默认值

                    currentStyle只有IE浏览器支持,其他的浏览器都不支持
                */
                // alert(box1.currentStyle.width);

                /*
                    在其他浏览器中可以使用
                        getComputedStyle()这个方法来获取元素的样式
                        这个方法是window的方法,可以直接使用
                    需要两个参数
                        第一个:要获取样式的元素
                        第二个:可以传一个伪元素,一般都传null

                    该方法返回一个对象,对象中封装来当前元素对应的样式
                        可以通过对象.样式来读取样式
                        如果获取的样式没有设置,则会获取到真实的值,而不是默认值
                        比如:没有设置width,它不会获取到auto,而是一个长度

                    但是该方法不支持IE8及以下的浏览器

                    通过currentStyle和getComputedStyle()读取到的样式都是只读的,
                        不能修改

                */
                // var obj = getComputedStyle(box1, null);
                // alert(obj);

                // 正常浏览器的方式
                // alert(getComputedStyle(box1, null).width);

                // IE8的方式
                // alert(box1.currentStyle.width);

                alert(getStyle(box1, "width"));
            }

            /*
                定义一个函数,用来获取指定元素当前的样式
                参数:
                    obj 要获取样式的元素
                    name 要获取的样式名
            */
            function getStyle(obj, name) {
                // if (window.getComputedStyle) {
                //     // 正常浏览器的方式
                //     return getComputedStyle(obj, null)[name];
                // }else {
                //     // IE8的方式
                //     return obj.currentStyle[name];
                // }

                // if (obj.currentStyle) {
                //     return obj.currentStyle[name];
                // } else {
                //     return getComputedStyle(obj, null)[name];
                // }

                return window.getComputedStyle ? getComputedStyle(obj, null)[name] : obj.currentStyle[name];
            }
        }
    </script>
</head>

<body>
    <button id="btn01">点我一下</button>
    <br /><br />
    <div id="box1"></div>
</body>

</html>

 其他样式相关的属性

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        body {
            height: 5000px;
        }

        #box1 {
            width: 100px;
            height: 100px;
            background-color: red;
            padding: 10px;
            border: 10px solid yellow;
        }

        #box2 {
            padding: 100px;
            background-color: #bfa;
        }

        #box3 {
            margin: 100px 0;
        }

        #box4 {
            width: 200px;
            height: 300px;
            background-color: #bfa;
            /* overflow: hidden; */
            overflow: auto;
        }

        #box5 {
            width: 450px;
            height: 600px;
            background-color: yellow;
        }
    </style>

    <script type="text/javascript">
        window.onload = function () {
            var box1 = document.getElementById("box1");
            var btn01 = document.getElementById("btn01");
            var box4 = document.getElementById("box4");

            btn01.onclick = function () {
                /*
                    clientWidth
                    clientHeight
                        这两个属性可以获取元素的可见宽度和高度
                        这些属性都是不带px的,返回都是一个数字,可以直接进行计算
                        会获取元素的宽度和高度,包括内容区和内边距
                        这些属性都是只读的,不能修改
                */
                // alert(box1.clientWidth);// 120
                // alert(box1.clientHeight);// 120
                // box1.clientHeight = 300;

                /*
                    offsetWidth
                    offsetHeight
                        获取元素的整个的宽度和高度,包括内容区、内边距和边框
                */
                // alert(box1.offsetWidth);//140

                /*
                    offsetParent
                        可以用来获取当前元素的定位父元素
                        会获取到离当前元素最近的开启了定位的祖先元素
                */
                var op = box1.offsetParent;
                // alert(op.id);

                /*
                    offsetLeft 
                        当前元素相对于其定位父元素的水平偏移量
                    offsetTop
                        当前元素相对于其定位父元素的垂直偏移量
                */
                // alert(box1.offsetLeft);// 100

                // alert(box4.clientHeight);// 300

                /*
                    scrollWidth
                    scrollHeight
                        可以获取元素整个滚动区域的宽度和高度
                */
                // alert(box4.scrollHeight);// 600
                // alert(box4.scrollWidth);// 450

                /*
                    scrollLeft
                        可以获取水平滚动条滚动的距离
                    scrollTop
                        可以获取垂直滚动条滚动的距离
                */

                // 当满足scrollHeight - scrollTop == clientHeight 说明垂直滚动条到底了
                alert(box4.scrollHeight - box4.scrollTop);// 600 - ...

            };

        }
    </script>
</head>

<body>
    <button id="btn01">点我一下</button>
    <br /><br />
    <div id="box3" style="position: relative;">
        <div id="box2" style="position: relative;">
            <div id="box1"></div>
        </div>
    </div>

    <div id="box4">
        <div id="box5"></div>
    </div>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值