31

First, I am a beginner. I want a step by step instruction.

I want to add a smooth background hover effect to my links in Wordpress

a {
  color:#000;}
a:hover {
  background-color: #d1d1d1; color:#fff;
}

I want the hover on links slow, instead of instant. Do I need any JavaScript or jQuery ? If so, please tell me how to do that.

Joe Pigott
  • 7,981
  • 6
  • 31
  • 43
anupal
  • 542
  • 1
  • 5
  • 8

5 Answers5

84

Since this is a cosmetic effect, it shouldn't be too vital that this fires. Given that, you might want to look at CSS 3 transformations.

a {
  color: #000;
  transition: background 0.5s linear;
}
a:hover {
  background-color: #d1d1d1;
  color: #fff;
}
<a href="http://example.com">Hover me</a>
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    What's the "-ms" stands for if it's not standing for microsoft? Thanks for this small piece of code. I knew transitions but it opened my eyes on how to use it. – nembleton May 03 '12 at 15:29
  • 1
    It's IE10 usually. So yes ms = microsoft but more advanced CSS features are unsupported below IE10. – Aron Sep 20 '13 at 11:00
1

The CSS3 Transition effect would work for what you are looking for. You can find more info on how to use it here: http://www.css3.info/preview/css3-transitions/

-4

Note: This was written before CSS transitions were widely available (they had just come out, and browser support was insufficient). If you were doing this today then use CSS transitions, and not javascript.

Yes, you need javascript. jQuery makes it easier.

I'm not so sure you should be doing that as a beginner, but:

You will need to include the jQuery library in a script tag:

<SCRIPT type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></SCRIPT>

Then:

<SCRIPT type="text/javascript">
$(function() {
  $('a').hover(
   function() { $(this).animate( { backgroundColor: '#d1d1d1', color: '#fff' } ) },
   function() { $(this).animate( { backgroundColor: '',        color: ''     } ) }
  );
});
</SCRIPT>
Ariel
  • 25,995
  • 5
  • 59
  • 69
  • 1
    afaik, i think you should also need jquery-ui to be able to animate background-color – guido Aug 09 '11 at 10:29
  • Use https://github.com/jquery/jquery-color . A jQuery plugin to control everything about colors. Very nice. ( I personaly prefer the CSS3 transitions though ) – nembleton May 03 '12 at 15:31
-4

You cannot animate the background color until you use a plug-in. The plug in is designed by the same guy who created jQuery though: http://plugins.jquery.com/project/color

He just didn't include it because it would have made the js file bigger.

Note: you can change the opacity though.

rkw
  • 7,287
  • 4
  • 26
  • 39
  • Many people contribute to JQuery, it's not just 'one guy'. Also, JQuery is *completely* overkill for this. – AStopher Aug 16 '16 at 16:11
-5
$(document).ready(function() { 
    var COLOR = {   
        fadeBackground: function(config){

            var totalStartPoint= config.startRED+config.startGREEN+config.startBLUE;
            var totelEndPoint  = config.endRED+config.endGREEN+config.endBLUE;
            if(totalStartPoint < totelEndPoint){
              var clearTime = setInterval(
                function (){
                    //elem.css("background-color", "rgb("+color.startRED+","+color.startGREEN+","+color.startBLUE+")");
                    document.getElementById('jsFullAccessColor').style.background ="rgb("+config.startRED+","+config.startGREEN+","+config.startBLUE+")";
                    if(config.startRED < config.endRED){ 
                            config.startRED++;
                            }
                    if(config.startGREEN < config.endGREEN){ 
                            config.startGREEN++;
                            }
                    if(config.startBLUE < config.endBLUE){ 
                            config.startBLUE++;
                            }
                      if(config.startRED == config.endRED && config.startGREEN == config.endGREEN && config.startBLUE == config.endBLUE){ 
                            clearTimer(clearTime);
                            }

                }, config.speed); 

                }

                if(totalStartPoint > totelEndPoint){
                    var clearTime = setInterval(
                    function (){

                        document.getElementById(config.element).style.background ="rgb("+config.startRED+","+config.startGREEN+","+config.startBLUE+")";
                        if(config.startRED > config.endRED){ 
                                config.startRED--;
                                }
                        if(config.startGREEN > config.endGREEN){ 
                                config.startGREEN --;
                                }
                        if(config.startBLUE > config.endBLUE){ 
                                config.startBLUE--;
                                }
                          if(config.startRED == config.endRED && config.startGREEN == config.endGREEN && config.startBLUE == config.endBLUE){               
                                clearTimer(clearTime);

                                }

                    }, config.speed); 

                 }
         }

    }

    function clearTimer(timerId){   
        clearInterval (timerId);
             }

    $(".domEleement").on("click",function (){

        var config ={
                //color starting point
                startRED:172,
                startGREEN:210,
                startBLUE:247,
                //color end point
                endRED:255,
                endGREEN:255,
                endBLUE:255,
                //element 
                element:"jsFullAccessColor",
                //speed
                speed:20

            }
            COLOR.fadeBackground(config);

    });


});
Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
Yene Mulatu
  • 2,226
  • 1
  • 17
  • 13