admin管理员组文章数量:1435859
I have the following HTML and jQuery code to warn users about using the 'remember me' check box as follows:
in HTML:
<input type="checkbox" id="remember" name="remember"> Remember me
<span id="remember_feedback"></span>
In Script:
$('#remember').change(function(){
if(this.checked){
$('#remember_feedback').text('(Don\'t use on a public puter)');
}else{
$('#remember_feedback').text('');
}
});
It works fine but I would like the text to smoothly / slowly changes as i've seen on some sites not pops in and out as it does now, is it possible without using plugins?
I have the following HTML and jQuery code to warn users about using the 'remember me' check box as follows:
in HTML:
<input type="checkbox" id="remember" name="remember"> Remember me
<span id="remember_feedback"></span>
In Script:
$('#remember').change(function(){
if(this.checked){
$('#remember_feedback').text('(Don\'t use on a public puter)');
}else{
$('#remember_feedback').text('');
}
});
It works fine but I would like the text to smoothly / slowly changes as i've seen on some sites not pops in and out as it does now, is it possible without using plugins?
Share Improve this question asked Apr 20, 2014 at 15:41 CarloCarlo 331 silver badge5 bronze badges 1- Are you meaning fading in/out? – Chris Brown Commented Apr 20, 2014 at 15:45
4 Answers
Reset to default 6you can do like this:
$('#remember').change(function(){
if(this.checked){
$('#remember_feedback').hide().text('(Don\'t use on a public puter)').fadeIn('slow');
}else{
$('#remember_feedback').fadeOut('slow');
}
});
JSFiddle example
Add the desired text to the html:
<span id="remember_feedback">(Don't use on a public puter)</span>
... then in css, hide it by default:
#remember_feedback {
display:none;
}
... then just use fadeIn
and fadeOut
in js:
$('#remember').change(function(){
if(this.checked){
$('#remember_feedback').fadeIn();
}else{
$('#remember_feedback').fadeOut();
}
});
Here is a DEMO you can play with.
As an alternative to the answers below, you can also use jQuery's .fadeToggle()
(including the addition of the message into the span in the HTML);
var fadeTime = 500; // Time (ms) for fade animation
$('#remember').change(function(){
$('#remember_feedback').fadeToggle(fadeTime);
});
JSFiddle
Add the text initially to the remember_feedback span and set it to be hidden by default using the html5 hidden attribute:
<span id="remember_feedback" hidden >Don't use on a public puter</span>
Then just show and hide it in the js:
$('#remember').change(function()
{
if(this.checked)
{
// Parameter is number of milliseconds to fade the element
$('#remember_feedback').fadeIn(1000);
}
else
{
$('#remember_feedback').fadeOut(1000);
}
});
本文标签: javascriptjQuery text change smoothlyStack Overflow
版权声明:本文标题:javascript - jQuery text change smoothly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745674201a2669750.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论