javascript - Angular toggle background-image -
i want change background-image of element when click it.
right have:
<div class="item-center text-center" toggle-class="active cable"> <div class="quiz-background"></div> <p class="size1 quiz-text">cable</p> </div> css:
.quiz-background { width: 80%; min-height: 100px; background-image: url(images/ofertas/cable_darkblue.png); background-position: center center; background-size: cover; background-repeat: no-repeat; margin: auto; top: 20px; position: relative; } .cable .quiz-background { background-image: url(images/ofertas/cable_lightblue.png); } directive toggle-class:
'use strict'; angular.module('libertyprapp') .directive('toggleclass', function() { return { restrict: 'ea', link: function(scope, elem, attrs) { elem.bind('click', function() { elem.toggleclass(attrs.toggleclass); }); } } }); right now, background image toggles because i'm toggling class second background. want able inline styling style="background-image: url(...)"and able change inline background style.
is there way angular?
thanks!
edit
here's i've done far, works i'm not if it's best way:
<div class="item-center text-center circle-image-quiz" toggle-class="active cable"> <div class="quiz-background" style="background-image: url(images/ofertas/cable_darkblue.png);"></div> <p class="size1 quiz-text">cable</p> </div> and css:
.quiz-background { width: 80%; min-height: 100px; background-position: center center; background-size: cover; background-repeat: no-repeat; margin: auto; top: 20px; position: relative; } .cable .quiz-background { background-image: url(images/ofertas/cable_white.png)!important; } this way toggles class background image overwriting inline styled one.
i've done cause i'll have more 1 of these elements uses different background images when clicked , not clicked.
you can setup in controller $scope.toggledactive , can use ng-class change background switching classes, whatever. toggledactive changed true or false based upon button click.
Comments
Post a Comment