问题
Having trouble getting my head around how I can recreate this effect as an on click as opposed to on hover?
I'm not great with jQuery so having trouble working out how I can trigger this to happen on click. I came across this website to help create the effect I'm going for (Underline Left to Right). The demo can be seen in the following link...
http://bradsknutson.com/blog/css-sliding-underline/
I'm currently using the following js to fade in a div and I want the line to slide in at them same time.
New jsfiddle - http://jsfiddle.net/tfgou78c/4/
JS:
$(function() {
$('.submit_button').click(function() {
var elem = $('.form_wrap');
if (elem.css('display') == 'none') {
elem.fadeIn(1750);
}
else {
elem.fadeOut(1750);
}
});
});
CSS:
.form_wrap {
display: none;
}
/* sliding underline LEFT TO RIGHT */
.sliding-u-l-r {
display: inline-block;
}
.sliding-u-l-r:after {
content: '';
display: block;
height: 3px;
width: 0;
background: transparent;
transition: width .5s ease, background-color .5s ease;
}
.sliding-u-l-r:hover:after {
width: 100%;
background: blue;
}
Any help would be greatly appreciated.
回答1:
Since this method are using the CSS event :hover
you can't change that for click
just on the code because there is no method for click
on CSS. You can have:
You tag this with Jquery so you can change the CSS match to:
.sliding-u-l-r:hover:after
to
.sliding-u-l-r.clicked:after
And then with Jquery:
$('.sliding-u-l-r').on('click',function(){ $(this).toggleClass('clicked'); })
Check this Demo Fiddle
回答2:
Something like http://jsfiddle.net/y4wuurt9/ ?
$('.underline-thing').on('click', function(e) {
e.preventDefault();
$(this).addClass('lined');
});
.underline-thing {
position: relative;
text-decoration: none;
}
.underline-thing:after {
content: "";
position: absolute;
bottom: 0;
left: 0;
width: 0;
height: 3px;
background: blue;
-webkit-transition: width 250ms ease-in-out;
-moz-transition: width 250ms ease-in-out;
-o-transition: width 250ms ease-in-out;
transition: width 250ms ease-in-out;
}
.underline-thing.lined:after {
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="underline-thing" href="#">Test Link</a>
回答3:
On click you wouldn't want it to stay would you?
Here's a pure css version that gets underlined on click:
a:active{
text-decoration:underline;
}
a{
text-decoration:none;
}
<a href="#">click me and hold!</a>
来源:https://stackoverflow.com/questions/26466949/animate-link-underline-on-click-as-opposed-to-on-hover