How can I use data() values in <style>..</style>? I tried several combinations (with/without this, with/without {{brackets}} etc. but can't get it to work. If I enter a value like '#f00' instead it works fine.
I built a template like so:
<template>
<ul class="striped">
<li>foo</li>
<li>bar</li>
<li>foobar</li>
</ul>
</template>
<style>
ul.striped> li:nth-of-type(odd) {
background-color: this.colors[0].backgroundcolor; //dynamic value from data()
}
</style>
<script>
data() {
return {
colors: [{'backgroundcolor':'#def'}], //simplified version for explanation
[..]
</script>
With the Vue 3.2 release you can just use State-Driven Dynamic CSS like that :
<template>
<ul class="striped">
<li>foo</li>
<li>bar</li>
<li>foobar</li>
</ul>
</template>
<style>
ul.striped> li:nth-of-type(odd) {
background-color: v-bind('colors[0]')
}
</style>
<script>
data() {
return {
colors: ['#def'],
}
}
</script>
If you're using Vue 2 you may use CSS custom properties like that :
<template>
<ul class="striped" :style="`--color: ${colors[0]}`">
<li>foo</li>
<li>bar</li>
<li>foobar</li>
</ul>
</template>
<style>
ul.striped> li:nth-of-type(odd) {
background-color: var(--color)
}
</style>
<script>
export default {
data() {
return {
colors: ['#def'],
}
}
}
</script>
Related
I have a menu including the following li
<div class="menu">
<ul>
<li>Home</li>
<li>News</li>
<li><a href="abc.jsp">ABC</li>
</ul>
</div>
In this code, home page is actived. But want to enable active status when I click on my News page. How Can I do? Thanks for watching.
Please add Js like:
jQuery(document).ready(function () {
jQuery('.menu li a').click(function () {
//removing the previous selected menu state
jQuery('.menu li').find('a.active').removeClass('active');
//adding the state for this parent menu
jQuery(this).addClass('active');
});
});
a.active {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="menu">
<ul>
<li>Home</li>
<li>News</li>
<li><a href="abc.jsp">ABC</li>
</ul>
</div>
You can use jQuery to do so, use the script below:
$(document).ready(function () {
$('.menu ul li a').click(function () {
// This will remove active class from other links
$('.menu ul li').find('a.active').removeClass('active');
// This will add active class to the link clicked
$(this).addClass('active');
});
});
Here is code using pure javascript
화이팅!!
function change(elem){
var list = document.querySelectorAll(".menu ul li a");
for (var i = 0; i < list.length; ++i) {
list[i].classList.remove('active');
}
elem.classList.add('active');
}
.active{
color:red;
}
<div class="menu">
<ul>
<li>Home</li>
<li>News</li>
<li>ABC</li>
</ul>
</div>
With some JS or JQuery , Add a click event on your links and call a method
<div class="menu">
<ul>
<li>Home</li>
<li>News</li>
<li><a href="abc.jsp" click="MyMethod">ABC</li>
</ul>
</div>
and in Jquery(or JS) :
perform this :
YourLinkClicked.removeClass('active');
YourLinkClicked.addClass('active');
Or just look at this link : http://jsfiddle.net/designaroni/E53t9/
Modifying #לבני מלכה's answer to
a) Be a11y
b) Work with frameworks (like svelte)
c) Use events instead of the element itself (better for accessibility).
d) Have JSDoc Comments (for whoever must revise my code when this is old and non-modern).
Here is the HTML markup (pure HTML) :
<div class="navbar">
<ul>
<li><a onclick="change(e)" class="active">Link 1</a></li>
<li><a onclick="change(e)">Link 2</a></li>
</ul>
</div>
Pure JS (w/ JSDoc):
/**
* This function is used in the navbar. It gives styles to the active link.
* #param {Event | Undefined} e Event (click event).
*/
const change = (e) => {
if (!e) e = window.event; // <-- for Chrome <89, Firefox (I forgot versions), and Safari.
// If you have been used to JS for a bit, you may think that {} needs to be used above, but no. In modern JS, this is unnecessary. Of course, for multi-line if statements, you do need the brackets.
let list = document.querySelectorAll(".navbar ul li a");
list.forEach(elem => {
elem.classList.remove("active");
});
// Note the below comment is only if you are using a type checker, if you aren't then you can remove this. (the comment).
// #ts-ignore
e.target.classList.add('active');
}
The best and easy way to do it:
Just listen event on parent of list (ul)
After click find old active element and remove class for it
Set active class for target of event
document.querySelector('ul').addEventListener('click', event => {
// remove old active
document.querySelector('.menu .active').classList.remove('active');
// set new active
event.target.classList.add('active');
})
a {
color: #ccc;
text-decoration: none;
}
.active {
color: blue;
}
<div class="menu">
<ul>
<li><a class="active">Home</a></li>
<li><a>News</a></li>
<li><a>ABC</a></li>
</ul>
</div>
Here is some basic function i use for my current MVC project.
Add this to the master page or shared pages, the js need to run each time the site reload.
and you need to use jquery
$(document).ready(function () {
// the current page url eg /index.jsp
var href = window.location.href.toLowerCase();
$(".menu").find("a").each(function () {
// find the current li that match the current Url
if (href.indexOf($(this).attr("href").toLowerCase()) != -1)
$(this).addClass("active"); // set the current li to active
})
})
.acive{
color:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="menu">
<ul>
<li>Home</li>
<li>News</li>
<li><a href="abc.jsp">ABC</li>
</ul>
</div>
Suppose one of my keys has value Green/Red and I want to show bootstrap btn-success when its value is green and btn-danger when its red.
You need to update ng-class
<div ng:controller="CartForm">
<ul>
<li ng-repeat='country in countries'><a class="btn" ng-class="{'btn-primary': country.color == 'green', 'btn-danger': country.color == 'red'}" href="">{{country.name}} - {{country.population}}</a></li>
</ul>
</div>
and delete appliedClass function
Working jsFiddle
I think you are looking for ng-class. You can use the following synthax:
<p ng-class="condition ? 'classIfTrue' : 'classIfFalse'">Foo</p>
In your case, it would be something like:
<p ng-class="myVariable ? 'btn-success' : 'btn-danger'">Foo</p>
As you did not provide a code sample, here is how it works with an example:
angular.module('app', []).controller('MyCtrl', function() {
this.valueSuccess = false;
this.valueDanger = true;
});
.danger { background-color: red; }
.success { background-color: green; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<section ng-app="app">
<div ng-controller="MyCtrl as vm">
<p ng-class="vm.valueSuccess ? 'danger' : 'success'"> {{vm.valueSuccess}}</p>
<p ng-class="vm.valueDanger ? 'danger' : 'success'">{{vm.valueDanger}}</p>
</div>
</section>
HTML:
<ul>
<li data-target="#front-info" data-slide-to="0" class="col-md-4 col-width-fix front-info-button">
<h1>Heading One</h1>
</li>
<li data-target="#front-info" data-slide-to="1" class="col-md-4 col-width-fix front-info-button">
<h1>Heading Two</h1>
</li>
<li data-target="#front-info" data-slide-to="2" class="col-md-4 col-width-fix front-info-button">
<h1>Heading Three</h1>
</li>
</ul>
CSS:
.front-info-button:hover, .front-info-button:active
{
background-color:#666;
}
It reacts to hover but it won't maintain the color after I've clicked and moved my mouse away. What am I missing?
You could add that behaviour inside javascript. The example below uses jquery to accomplish this:
css
.selected {
background-color:#666;
}
js
$('ul li').click(function() {
$('ul li').removeClass('selected');
$(this).addClass('selected');
});
jsfiddle demo - http://jsfiddle.net/8ZvsF/
You can make it using jQuery:
$('li').hover(function(){
$(this).addClass("dark")
});
And the dark class:
.dark {
background-color:#666;
}
Here is a demo: http://jsfiddle.net/6nPu3/
In the event you're not using jQuery, try this JavaScript vanilla solution.
for (var i = 0, item = document.body.getElementsByTagName('li'); i < item.length; i++){
(item[i].attachEvent) ? item[i].attachEvent('onclick', function (){
item[i].className = item[i].class + ' ' + 'active';
}) : item[i].addEventListener('click', function () {
this.className = this.class + ' ' + 'active';
}, false);
};
I'm trying to implement CSS3 selectors using :target for a drawer animation, without luck.
Using $locationProvider.html5Mode(false); the animations is not working. Turning that feature to "true" is allowing me :target selector but the routing is not working and displaying anything.
angular.module('myApp', [
'ngRoute',
'myApp.filters',
'myApp.services',
'myApp.directives',
'myApp.controllers',
'myApp.services'
]).
config(['$locationProvider', '$routeProvider', function($locationProvider, $routeProvider) {
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});
$routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'});
$routeProvider.otherwise({redirectTo: '/view1'});
$locationProvider.html5Mode(false);
}]);
...
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
Anyone has a suggestion or idea?
Use a reference app to get started, and ng-animate to integrate the animation:
<html ng-app="demo">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" href="https://rawgithub.com/daneden/animate.css/master/animate.css">
<style>
a {
font-size: 300%;
}
#Squidward:target { color: red }
</style>
<script data-require="angular.js#1.1.5" data-semver="1.1.5" src="http://code.angularjs.org/1.1.5/angular.min.js"></script>
<script>
angular.module('demo', [
]).controller('MainCtrl', function(
$scope
){
$scope.items = [{
name: 'Squidward'
}, {
name: 'Plankton'
}];
$scope.add = function() {
$scope.items.push({
name: 'Patrick'
});
};
});
</script>
</head>
<body ng-controller="MainCtrl">
<div>
Add More Patrick
<ul>
<li ng-repeat="item in items" ng-animate="{
enter: 'animated flip'
}">
<a id={{item.name}}>{{item.name}}</a>
</li>
</ul>
</div>
</body>
</html>
References
AngularJS Wiki: JSFiddle Examples
AngularJS ng-animate + target Plunker Demo
Getting started with AngularJS
Angular JS 1.2x Developer Guide:Internet Explorer Compatibility
Firefox OS Simulator
Todo App for Firefox OS
Is there a way to define styles for a combination of classes? For example, I'd like my HTML to look like this, but the output to render in the appropriate color:
<span class="red">Red Text</span><br/>
<span class="green">Green Text</span><br/>
<span class="red green">Yellow Text</span><br/>
Edit: The above seems to be confusing people when it was just an example; so here is another example:
<style>
.style1 { background-color: #fff; }
.style2 { background-color: #eee; }
.style1.highlight { color: red; }
.style2.highlight { color: blue; }
</style>
<ul>
<li class="action style1">Do Action 1</li>
<li class="action style2">Do Action 2</li>
<li class="action style1 highlight">Do Action 1</li>
<li class="action style2 highlight">Do Action 2</li>
</ul>
<script language="javascript" type="text/javascript">
$("li.action").bind("click", function(e) {
e.preventDefault();
// Do some stuff
$(this).addClass("highlight");
$(this).unbind("click");
});
</script>
Again, this is just an example, so don't get hung up on alternating elements or anything like that. What I'm trying to avoid is having to duplicate the bind function for each different styleN or having to write an elseif structure that checks for each styleN class. Unfortunately this code doesn't work in IE 6 or 7 - the highlighted text for both .style1 and .style2 elements end up being blue.
You can select on multiple classes:
span.red.green { color: yellow; }
That will apply to any span element with red and green classes. Which may not be what you want, since it will also apply to, say:
<span class="red green blue">white</span>
Note that this doesn’t work right in IE 6.
Sure.
.red { color: red; }
.green { color: green; }
.red.green { color: yellow; }
You could do this using Javascript, although I it would be better just to create an yellow class, if your interested into the javascript method please comment (I need to think it out, and it would be more efficient just to already do this in your logic layer)
EDIT | Even more edit (added regex for current colors)
Okay, you're not interested but I'm still going to write it because it seems like fun:
var re = /([0-9]?)/g;
$("span").each( function(){
$this = $(this);
results = re.exec( $this.css('background-color')); // background is something like: 'rgb( 0, 132, 12)'
var sRed = results[1];
var sGreen = results[2];
var sBlue = results[3];
/* OLD!
var sRed = '0';
var sGreen = '0';
var sBlue = '0';*/
if($this.hasClass('red')) { sRed = '255'; }
if($this.hasClass('green')) { sGreen = '255'; }
if($this.hasClass('blue')) { sBlue = '255'; }
$this.css('background-color', 'rgb(' + sRed + ',' + sGreen + ',' + sBlue ')';
});
You will need jQuery for that, although you could probably do it without jQuery.
why do you want to do this? it may be possible, but i don't see the benefit. Would you also define class="short fat" and class="tall thin" or what about class="light dark"? Classes should be simple and specific for clarity and reuse. Multiple inheritance (fake or otherwise) should be avoided when it is unnecessary and potentially confusing.
class="red green"
is confusing
class="yellow"
is concise and clear
EDIT: i saw the second example, my advice remains unchanged. One class is concise and clear, two is potentially confusing. Assuming that this is supposed to work (and I think it is), expending great effort fighting bugs in the most popular browser (IE) is probably not worthwhile.
Here is my workaround:
<style>
.style1 { background-color: #fff; }
.style2 { background-color: #eee; }
.style1 .highlight { color: red; }
.style2 .highlight { color: blue; }
</style>
<ul>
<li class="action style1"><span>Do Action 1</span></li>
<li class="action style2"><span>Do Action 2</span></li>
<li class="action style1"><span class="highlight">Do Action 1</span></li>
<li class="action style2"><span class="highlight">Do Action 2</span></li>
</ul>
<script language="javascript" type="text/javascript">
$("li.action").bind("click", function(e) {
e.preventDefault();
// Do some stuff
$(this).children("span").addClass("highlight");
$(this).unbind("click");
});
</script>
It's not as elegant as I'd hoped. It uses an extra element for each item; but at least it's fairly clean still.
maybe something like this
.red
{
color: red;
}
.red_green
{
color: #AS8324;
}
and you can use your html code
<span class="red">Red Text</span><br/>
<span class="green">Green Text</span><br/>
<span class="red_green">Yellow Text</span><br/>
I don't know this is what you exactly want but this is my approach. I hope it helps.