I found myself stuck with this:
I have an anchor link that points to an <a>
inside a div
, so the page scrolls all the way down to it.
Unfortunately, the div
is at the bottom of the page, so usermost likely won't see it.
I thought a good way to solve this would be changing the class of the div when the link is clicked, for example switching the border color to red and then fade back to normal in 2 seconds.
I have no clue on how to do this. I Googled around and it seems this can be done with jQuery, but I really don't understand how to edit the scripts to my needs.
Thanks a lot!
To highlight active HTML anchor with CSS, use the :target selector.
Definition and Usage The :visited selector is used to select visited links. Tip: Use the :link selector to style links to unvisited pages, the :hover selector to style links when you mouse over them, and the :active selector to style links when you click on them.
The click method is intended to be used with INPUT elements of type button, checkbox, radio, reset or submit. Gecko does not implement the click method on other elements that might be expected to respond to mouse–clicks such as links (A elements), nor will it necessarily fire the click event of other elements.
The answer is definitely yes, but you will need to write javascript code for it. We can use a click handler on the div element to make it clickable.
Use this method if you don't really care about supporting all browsers. It's pure CSS, so that's an advantage. Here's an outline (includes multiple versions of rules for multiple browsers):
.youranchorsclass:active ~ #yourdivsid { /*when the anchor is active (clicked)*/
-moz-animation: myanimation 1s;
-webkit-animation: myanimation 1s;
-o-animation: myanimation 1s;
animation: myanimation 1s;
}
@-moz-keyframes myanimation, @-webkit-keyframes myanimation, @-o-keyframes myanimation, @keyframes myanimation {
from { background: red; }
to { background: white; /*or whatever it was originally*/ }
}
(If you want to get rid of all those ugly prefixed rules, take a look at PrefixFree).
Use this if you do care about older-browsers support. Include jQuery in your page, to start with, by inserting this into your head
:
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type = "text/javascript"></script>
Then:
$(".yourlink").click(function() {
$("#yourdivid").css("background", "red").delay(1000).css("background", "white");
};
Note that this jQuery method doesn't gradually change the color, you'd have to include a plugin (such as jQuery UI) to do so.
Use this if you don't want to include a relatively-huge library just for such a small effect. It's pretty straightforward, here's a commented outline to get you started:
function changeDivColor(color) {
document.getElementById("yourdivid").style.backgroundColor = color;
}
document.getElementById("youranchor").onClick = function() { //when the anchor is clicked
changeDivColor("red"); //chang the div color to red
setTimeout(function() { //wait 1000 milliseconds (1s) -- see below
changeDivColor("white"); //then change it back to white
}, 1000);
};
Hope that helped in any manner!
Yes, you can do the Yellow Fade Trick in two ways:
:target
pseudo class:<section id="voters">
Content
</section>
Respective CSS
:target {
background: yellow;
}
In the click function, if you have, you can do this way:
$('a[href*="#"]').click(function(){
$($(this).attr("href")).effect("highlight", {}, 1500);
});
Or using animate()
:
$('a[href*="#"]').click(function(){
$($(this).attr("href")).animate({"background-color": "#ffc"}).delay(2000).animate({"background-color": "transparent"});
});
PS: For using effect()
, You need to have these two JS: effects.core.js
and effects.highlight.js
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With