使用javaScript控制伪元素

来源:互联网 发布:中国微观数据库简介 编辑:程序博客网 时间:2024/05/22 04:32

一、伪元素并不是dom元素,js是无法直接操作它的
伪元素有六个,分别是 ::after 、 ::before、 ::first-line、 ::first-letter、 ::selection、 ::backdrop。 常用的是 ::after 、 ::before。
而且在css3中伪元素要使用两个::的语法。而不是一个冒号(:),目的是为了区分伪类和伪元素。只有 ::selection 永远只能以两个冒号开头(::)。因为IE8只支持单冒号的语法,所以,如果你想兼容IE8,保险的做法是使用单冒号。


二、获取伪元素的属性值
通过 window.getComputedStyle() 方法来获取伪元素css样式声明对象。用getPropertyValue方法或直接使用键值访问获取对应的属性值。
语法:window.getComputedStyle(element[, pseudoElement])
element(Object):伪元素的所在的DOM元素;
pseudoElement(String):伪元素类型。可选值有:”:after”、”:before”、
”:first-line”、”:first-letter”、”:selection”、”:backdrop”;


三、演示

//css#active:before{content: "hello world!";display: block;width: 100px;height: 100px;background: red;}//HTML<div id='active'></div>//js代码var myId=document.getElementById('active');var beforeStyle=window.getComputedStyle(myId,':before');console.log(beforeStyle); // [CSSStyleDeclaration Object]console.log(beforeStyle.width); // 100pxconsole.log(beforeStyle.getPropertyValue("width")); // 100pxconsole.log(beforeStyle.content); // "hello world!"

四、备注:
1.getPropertyValue()和直接使用键值访问,都可以访问CSSStyleDeclaration Object。它们两者的区别有:
对于float属性,如果使用键值访问,则不能直接使用getComputedStyle(element, null).float,而应该是cssFloat与styleFloat;
直接使用键值访问,则属性的键需要使用驼峰写法,如:style.backgroundColor;
使用getPropertyValue()方法不必可以驼峰书写形式(不支持驼峰写法),例如:style.getPropertyValue(“border-top-color”);
getPropertyValue()方法在IE9+和其他现代浏览器中都支持;在IE6~8中,可以使用getAttribute()方法来代替;
2.伪元素默认是”display: inline”。如果没有定义display属性,即使在CSS中显式设置了width的属性值为固定的大小如”100px”,但是最后获取的width值仍是”auto”。这是因为行内元素不能自定义设置宽高。解决办法是给伪元素修改display属性为”block”、”inline-block”或其他。


五、具体案例

  • 可以使用一套已经写好的伪元素样式,来替代原有的样式
$(".red").removeClass('red').addClass('green');
  • 用CSStyleSheet的insertRule来为伪元素修改样式
// 支持IE document.styleSheets[0].addRule('.red::before','color: green'); // 支持非IE的现代浏览器document.styleSheets[0].insertRule('.red::before { color: green }', 0); 
  • 在 head标签中插入 style 的内部样式
var style = document.createElement("style"); document.head.appendChild(style); sheet = style.sheet;  // 兼容IE浏览器sheet.addRule('.red::before','color: green'); // 支持非IE的现代浏览器sheet.insertRule('.red::before { color: green }', 0);

或者使用jQuery:

$('<style>.red::before{color:green}</style>').appendTo('head');

六、修改伪元素的content的属性值

  • 使用CSSStyleSheet的insertRule来为伪元素修改样式
var latestContent = "修改过的内容";var formerContent = window.getComputedStyle($('.red'), '::before').getPropertyValue('content');  // 兼容IE浏览器document.styleSheets[0].addRule('.red::before','content: "' + latestContent + '"');  // 支持非IE的现代浏览器document.styleSheets[0].insertRule('.red::before { content: "' + latestContent + '" }', 0);
  • 使用DOM元素的data-*属性来更改content的值(这个方法我喜欢)
// CSS代码.red::before {content: attr(data-attr);color: red;}// HTML代码<div class="red" data-attr="red">内容内容内容内容</div>// JacaScript代码$('.red').attr('data-attr', 'green');

七、:before和:after伪元素的常见用法总结

  • 利用content属性,为元素添加内容修饰
//使用引号包括一段字符串,将会向元素内容中添加字符串a:after { content: "after content"; }
  • 使用attr()方法,调用当前元素的属性的值
a:after { content: attr(href); }a:after { content: attr(data-attr); }
  • 使用url()方法,引用多媒体文件
a::before { content: url(logo.png); }
  • 使用counter()方法,调用计时器
h:before { counter-increment: chapter; cotent: "Chapter " counter(chapter) ". " }
  • 清除浮动
.clear-fix { *overflow: hidden; *zoom: 1; }.clear-fix:after { display: table; content: ""; width: 0; clear: both; }
  • 特效妙用
// CSS代码a {position: relative;display: inline-block;text-decoration: none;color: #000;font-size: 32px;padding: 5px 10px;}a::before, a::after { content: "";transition: all 0.2s;}a::before { left: 0;}a::after { right: 0;}a:hover::before, a:hover::after { position: absolute;}a:hover::before { content: "\5B"; left: -20px; }a:hover::after { content: "\5D"; right: -20px; }// HTML代码<a href="#">我是个超链接</a>
  • 特殊形状的实现
// CSS代码  对话气泡.tooltip {position: relative;display: inline-block;padding: 5px 10px;background: #80D4C8;}.tooltip:before {content: "";display: block;position: absolute;left: 50%;margin-left: -5px;bottom: -5px;width: 0; height: 0; border-left: 5px solid transparent;border-right: 5px solid transparent;border-top: 5px solid #80D4C8;}// HTML代码<div class="tooltip">I'm a tooltip.</div>

:before 和 :after 伪元素结合更多CSS3强大的特性,还可完成非常多有趣的特效和 hack ,这里权当抛砖引玉

总结
伪元素的content属性很强大,可以写入各种字符串和部分多媒体文件。但是伪元素的内容只存在于CSS渲染树中,并不存在于真实的DOM中。所以为了SEO优化,最好不要在伪元素中包含与文档相关的内容。

修改伪元素的样式,建议使用通过更换class来修改样式的方法。因为其他两种通过插入行内CSSStyleSheet的方式是在JavaScript中插入字符代码,不利于样式与控制分离;而且字符串拼接易出错。

修改伪元素的content属性的值,建议使用利用DOM的data-*属性来更改。

声明:经过多个网上对伪元素总结的文章再次进行总结。

0 0
原创粉丝点击