[英]Hide class pseudo-elements with JavaScript
How to show/hide class :before and :after pseudo elements with JavaScript? 如何使用JavaScript显示/隐藏类:before和:after伪元素?
I have arrows on both sides of div. 我在div的两边都有箭头。 Below is arrows style
下面是箭头样式
.edit-wrap:before,
.edit-wrap:after {
@media (max-width: 768px) {
content: '';
....
}
}
I want to hide them, if, for example, there is no scroll bar in the element. 我想隐藏它们,例如,如果元素中没有滚动条。 And show otherwise
否则显示
var scrollableElement = document.getElementById('scrollable-element');
if(scrollableElement.scrollWidth>scrollableElement.clientWidth){
//display arrows
} else{
//hide arrows
}
I am not using JQuery 我没有使用JQuery
You may remove the class which makes use of the pseudo classes :before
and :after
. 您可以删除使用伪类的类
:before
和:after
。 With javascript you can achieve this by using the className
property (assuming there are no other classes for the target). 使用javascript,您可以使用
className
属性来实现此目的(假设目标没有其他类)。
var scrollableElement = document.getElementById('scrollable-element');
if (scrollableElement.scrollWidth > scrollableElement.clientWidth) {
document.getElementById('yourElementId').className = 'edit-wrap';
} else {
document.getElementById('yourElementId').className = '';
}
If you want to remove the specific class, you should have a look to classList
, which is supported by most modern browsers . 如果要删除特定的类,则应查看
classList
, 大多数现代浏览器都支持 classList
。
var scrollableElement = document.getElementById('scrollable-element');
if (scrollableElement.scrollWidth > scrollableElement.clientWidth) {
document.getElementById('yourElementId').classList.add('edit-wrap');
} else {
document.getElementById('yourElementId').classList.remove('edit-wrap');
}
Another approach for older browsers would be using regular expressions . 旧版浏览器的另一种方法是使用正则表达式 。
I think you need to use CSS and JavaScript here. 我认为您需要在此处使用CSS和JavaScript。
CSS CSS
.hide-before:before, .hide-after:after { display:none; }
JavaScript JavaScript的
var scrollableElement = document.getElementById('scrollable-element');
if (scrollableElement.scrollWidth>scrollableElement.clientWidth) {
scrollableElement.classList.add('hide-after','hide-before');
} else {
scrollableElement.classList.remove('hide-after','hide-before');
}
Add, for example, .hide-pseudo class to element and edit your styles like this: 例如,将.hide-pseudo类添加到element并编辑您的样式,如下所示:
.edit-wrap.hide-pseudo:before,
.edit-wrap.hide-pseudo:after {
disply: none;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.