jQuery possible optimize custom hover code -


i have menu , each list item gets different color border on hover. works feels clunky. how can optimize code don't repeat much.

$('ul>li:first-child>a').hover(function() {    $(this).css('border-bottom', '0.2em solid blue');  }, function() {    $(this).css('border-bottom', '0');  })    $('ul>li:nth-child(2)>a').hover(function() {    $(this).css('border-bottom', '0.2em solid green');  }, function() {    $(this).css('border-bottom', '0');  })    $('ul>li:last-child>a').hover(function() {    $(this).css('border-bottom', '0.2em solid yellow');  }, function() {    $(this).css('border-bottom', '0');  })
ul li {    display: inline-block;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <ul>    <li><a href="#">blue</a></li>    <li><a href="#">green</a></li>    <li><a href="#">yellow</a></li>  </ul>

you can achieve entirely using css , html. add class list elements , target them using css style.

html:

<ul>   <li class="blue"><a href="#">blue</a></li>   <li class="green"><a href="#">green</a></li>   <li class="yellow"><a href="#">yellow</a></li> </ul> 

css:

ul li {   display: inline-block; } .blue a:hover{     color:#0000ff; } .green a:hover {     color:#00ff00; } .yellow a:hover {     color:#ffff00; } 

check fiddle here: https://jsfiddle.net/rnjr1al9/

edit: have css text color can target different things border-color, font-size, etc.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -