обращение к псевдоклассам CSS через jQuery
Добрый день уважаемые. Возник такой вопрос, есть ли возможность обращения к псевдоклассам стилей CSS через jQuery, и работать с их параметрами ? Вот простой пример
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<style type="text/css">
#field {
position: relative;
width: 400px;
height: 400px;
margin-left: 10%;
margin-top: 10%;
background-color: yellow;
border: 3px solid black;
}
.block {
float: left;
margin-left: 5%;
margin-top: 20%;
width: 40px;
height: 40px;
background-color: rgba(0, 0, 255, 1);
border: 1px solid black;
}
.block:before {
content: "";
position: absolute;
height: 0;
width: 0;
top: 40px;
border-width: 0 21.5px 40px;
border-style: solid;
/*border-color: transparent transparent rgba(0, 0, 255, 0.5);*/
*!* border-bottom-color: rgba(0, 255, 0, 1); */!*
border-left-color: transparent;
border-right-color: transparent;
border-top-color: transparent;
}
</style>
<body>
<div id="field">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
<input id="button" type="button" value="Push me!">
</body>
<script type="text/javascript">
window.onload = function () {
var i, r, g, b,
block = $('.block'),
*!* blockBef = $('.block:before'),*/!*
allBlock = $('#field > div'),
len = allBlock.length,
but = $('#button');
but.on('click', allBlock, function () {
console.log(allBlock.length + '=blocks');
r = 255;
g = 0;
b = 0;
block.css({backgroundColor: "rgba(" + r + ", " + g + ", " + b + ", " + 0.5 + ")"});
*!* blockBef.css({borderBottomColor: "rgba(" + r + ", " + g + ", " + b + ", " + 0.5 + ")"});*/!*
})
}
</script>
</html>
Вопрос, есть ли возможность поменять цвет треугольничкам по клику ? |
Black_Star,
Через jquery это больно. Делай по-другому: 1) У тебя в стилях есть твой элемент и элемент:before 2) Короче делаешь так элемент.класс_поведения:befor e То есть вешаешь классы на элемент и прописываешь как должен выглядеть :before в стилях |
можно до таблицы стилей достучаться
<!doctype html>
<head>
<style id="myStyle">
.test:before {
content: 'before content';
color: red;
}
</style>
<script>
function ChangeCss() {
var styleTag = document.getElementById("myStyle"),
sheet = styleTag.sheet ? styleTag.sheet : styleTag.styleSheet,
rules = sheet.cssRules ? sheet.cssRules : sheet.rules,
rule = rules[0];
rule.style.color = "green";
}
</script>
</head>
<body>
<div class=test>
div content
<div>
<button onclick="ChangeCss()">Поменять цвет псевдоэлемента!</button>
</body>
|
Цитата:
Цитата:
|
Цитата:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#field {
position: relative;
width: 400px;
height: 400px;
margin-left: 10%;
margin-top: 10%;
background-color: yellow;
border: 3px solid black;
}
.block {
float: left;
margin-left: 5%;
margin-top: 20%;
width: 40px;
height: 40px;
background-color: rgba(0, 0, 255, 1);
border: 1px solid black;
}
.block:before {
content: "";
position: absolute;
height: 0;
width: 0;
top: 40px;
border-width: 0 21.5px 40px;
border-style: solid;
/*border-color: transparent transparent rgba(0, 0, 255, 0.5);*/
border-bottom-color: rgba(0, 255, 0, 1);
border-left-color: transparent;
border-right-color: transparent;
border-top-color: transparent;
}
*!*.red{
background-color: rgba(255, 0, 0, 0.5);
}
.red:before{
border-bottom-color: rgba(255, 0, 0, 0.5);
}*/!*
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var i, r, g, b,
block = $('.block'),
allBlock = $('#field > div'),
len = allBlock.length,
but = $('#button');
but.on('click', allBlock, function () {
console.log(allBlock.length + '=blocks');
block.addClass('red')
})
})
</script>
</head>
<body>
<div id="field">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
<input id="button" type="button" value="Push me!">
</body>
</html>
Цитата:
Кстати конкретно вашу задачу можно решить использовав назначение цвета текста, оставив цвет бордера по умолчанию:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#field {
position: relative;
width: 400px;
height: 400px;
margin-left: 10%;
margin-top: 10%;
background-color: yellow;
border: 3px solid black;
}
.block {
float: left;
margin-left: 5%;
margin-top: 20%;
width: 40px;
height: 40px;
background-color: rgba(0, 0, 255, 1);
border: 1px solid black;
*!*color: rgba(0, 255, 0, 1);*/!*
}
.block:before {
content: "";
position: absolute;
height: 0;
width: 0;
top: 40px;
border-width: 0 21.5px 40px;
border-style: solid;
/*border-color: transparent transparent rgba(0, 0, 255, 0.5);*/
border-left-color: transparent;
border-right-color: transparent;
border-top-color: transparent;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var i, r, g, b,
block = $('.block'),
allBlock = $('#field > div'),
len = allBlock.length,
but = $('#button');
but.on('click', allBlock, function () {
console.log(allBlock.length + '=blocks');
r = 255;
g = 0;
b = 0;
block.css({
backgroundColor: "rgba(" + r + ", " + g + ", " + b + ", " + 0.5 + ")",
*!*color: "rgba(" + r + ", " + g + ", " + b + ", " + 0.5 + ")"*/!*
});
})
})
</script>
</head>
<body>
<div id="field">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
<input id="button" type="button" value="Push me!">
</body>
</html>
|
Black_Star,
mySelector {
/*...*/
}
mySelector:before {
/*...*/
}
mySelector .bg-black {
/*...*/
}
mySelector .bg-black:before {
/*...*/
}
mySelector .text-green {
/*....*/
}
mySelector .text-green:before {
/*....*/
}
|
Всем спасибо, за помощь.
Aetae к сожалению первый вариант не подошел, поскольку класс red меняет цвет и домикам(родителям), а у меня есть необходимость разделять цвета, крыш и домиков. Но вот второй вариант, подошел. Coriolan161 Спасибо, так понятнее |
| Часовой пояс GMT +3, время: 10:24. |