非内联css样式获取方法

css样式分为内联样式、内部样式和外部样式,然而object.style.xxx只能够获取内联样式的属性值,内部样式和外部样式的css样式获取不到

js获取方法使用document.defaultView.getComputedStyle()、currentStyle()

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4   <meta charset="UTF-8">
 5   <!-- <meta name="viewport" content="width=device-width, initial-scale=1.0">
 6   <meta http-equiv="X-UA-Compatible" content="ie=edge"> -->
 7   <title>Promise animation</title>
 8   <style>
 9     .ball{
10       width: 40px;
11       height: 40px;
12       border-radius: 20px;
13       margin-left: 10px;
14     }
15     .ball1{
16       background: red;
17     }
18     .ball2{
19       background: yellow;
20     }
21     .ball3{
22       background: green;
23     }
24   </style>
25 </head>
26 <body>
27   <div class="ball ball1"></div>
28   <div class="ball ball2"></div>
29   <div class="ball ball3"></div>
30   <script type="text/javascript">
31     var ball1=document.querySelector(‘.ball1‘);
32     var ball2=document.querySelector(‘.ball2‘);
33     var ball3=document.querySelector(‘.ball3‘);
34
35     //用于获取外部引入的属性值,解决margin-left未写在行内获取为空的情况
36     function getCurrentstyle(obj,prop){
37       if (obj.currentStyle) //IE
38       {
39           return obj.currentStyle[prop];
40       }
41       else if (window.getComputedStyle) //非IE
42       {
43           property = prop.replace(/([A-Z])/g, "-$1");
44           property = prop.toLowerCase();
45           return document.defaultView.getComputedStyle(obj,null)[property];
46       }
47       return null;
48     }
49
50     function animate(ball,distance,cb){
51       setTimeout(function(){
52         var marginLeft=parseInt(getCurrentstyle(ball,‘margin-left‘),10);
53         if (marginLeft===distance) {
54           cb && cb();
55         }else {
56           if (marginLeft<distance) {
57             marginLeft++;
58           }else {
59             marginLeft--;
60           }
61           ball.style.marginLeft=marginLeft+‘px‘;
62           animate(ball,distance,cb)
63         }
64       },13)
65     }
66     animate(ball1,100,function(){
67       animate(ball2,200,function(){
68         animate(ball3,300,function(){
69           animate(ball3,150,function(){
70             animate(ball2,150,function(){
71               animate(ball1,150,function(){
72                 //
73               })
74             })
75           })
76         })
77       })
78     })
79   </script>
80 </body>
81 </html>
时间: 2024-12-25 20:48:51

非内联css样式获取方法的相关文章

JS中获取CSS样式的方法

1.对于内联样式,可以直接使用ele.style.属性名(当然也可以用键值对的方式)获得.注意在CSS中单词之间用-连接,在JS中要用驼峰命名法 如 <div id="dv" style="width: 100px;height: 200px;background-color: pink; border: 1px solid green;"></div> <script> var dv = document.getElementB

C++中的内联成员函数与非内联成员函数

在C++中内联成员函数与非内联成员函数的可以分为两种情况: 1.如果成员函数的声明和定义是在一起的,那么无论有没有写inline这个成员函数都是内联的,如下: using namespace std; class test{ public: void fuc() { cout << "ok!" << endl; } }; int main(void) { test t, t1; t.fuc(); t1.fuc(); return 0; } 或者: using n

HTML文档中应用css样式的方法总结

在HTML文档中应用css样式大致有三种方法:1.link标签链接外部样式表:2.使用style元素包含样式表:3.使用style属性,即内联样式 一.link标签链接外部样式表 先看一条较为标准的link标记语句: <link rel="stylesheet" type="text/css" href="main.css" media="all" /> link标记必须放在head元素中,且不能放在其他元素(如t

用JS读写CSS样式的方法总结

为了日后方便查询,本人翻阅了一些资料总结了以下方法,如有不对的地方欢迎指出! 一.可以通过DOM节点对象的style对象(即CSSStyleDeclaration对象)来读写文档元素的CSS样式    如:var elm = document.getElementById('test'); elm.style.color = 'black'; 二.通过Element对象的getAttribute().setAttribute().removeAttribute()直接读写style属性    如

用原生JS读写CSS样式的方法总结

一.可以通过DOM节点对象的style对象(即CSSStyleDeclaration对象)来读写文档元素的CSS样式    如:var elm = document.getElementById('test'); elm.style.color = 'black'; 二.通过Element对象的getAttribute().setAttribute().removeAttribute()直接读写style属性    如:elm.setAttribute('style','color:red;li

css样式获取,style,currentStyle,getComputedStyle

对于css样式的获取问题,对于行内样式,我们可以用style来获取,但是对于内嵌和外部样式的话,style就心有余和力不足了.它是获取不到这些样式的 此时就只有currentStyle和getComputedStyle上阵了. currentStyle是只兼容各种IE的,但是不兼容火狐,谷歌的,而getComputeStyle的话是可以兼容火狐,谷歌,和IE9+的.(以下测试均在谷歌和IE下) 对于行内样式: <!DOCTYPE html> <html lang="en&quo

webpack4对第三方库css,项目全局css和vue内联css文件提取到单独的文件(二十二)

在讲解提取css之前,我们先看下项目的架构如下结构: ### 目录结构如下: demo1 # 工程名 | |--- dist # 打包后生成的目录文件 | |--- node_modules # 所有的依赖包 | |--- app | | |---index | | | |-- views # 存放所有vue页面文件 | | | | |-- index.vue | | | | |-- list.vue | | | |-- components # 存放vue公用的组件 | | | |-- js

jQuery_操作Css样式的方法

//1.获取和设置样式 $("#tow").attr("class")获取ID为tow的class属性 $("#two").attr("class","divClass")设置Id为two的class属性. //2.追加样式 $("#two").addClass("divClass2")为ID为two的对象追加样式divClass2 //3.移除样式 $("

div style标签内嵌CSS样式

我们在DIV标签内.SPAN标签内.p标签等html标签内使用style属性直接设置div的样式. 一.在<div>标签内使用style设置css样式   -   TOP 1.实例html源代码片段: <div style="font-size:14px; color:#F00">设置字体大小14px,颜色为红色</div> 2.div标签内使用style设置样式截图 div标签内设置样式截图 二.在<span>标签内使用style设置c