JQuery 淡入淡出按钮不起作用

发布于 2024-11-05 19:44:39 字数 961 浏览 1 评论 0原文

我有一个带有保存按钮的表单。单击“保存”按钮时,它会运行 ajax 来保存表单内容。那行得通。我遇到的麻烦是保存按钮淡入淡出。保存按钮的文本是“另存为草稿”。单击时,我希望它淡出,将文本更改为“草稿保存于[时间]”,然后淡入以显示它已保存以及保存的时间。几秒钟后,我希望该按钮消息淡出并被原始的“另存为草稿”按钮取代。

我想我必须将淡入淡出嵌套到函数中,但我的 jquery 淡入淡出天赋很小,而且很快就消失了:)

下面是 ajax 的成功部分......

success: function(data) {
    var date = new Date(),
    hours = date.getHours(),
    minutes = date.getMinutes();

    if (hours > 12) hours = hours - 12;
    if (minutes < 10) minutes = '0' + minutes;

    var save_draft = '<div id="save-button" class="button-clone">Save as Draft</div>';
    var draft_saved = '<div id="save-button" class="button-clone">Draft Saved at ' + hours + ':' + minutes + '</div>';

    $('#save-button').fadeOut(1000);
    $('#save-button').replaceWith(draft_saved);
    $('#save-button').fadeIn(1000);
    $('#save-button').fadeOut(1000);
    $('#save-button').replaceWith(save_draft);
    $('#save-button').fadeIn(1000);
}

I have a form which has a save button. When the save button is clicked it runs ajax to save the form contents. That works. What I am having troubles with is the save button fadein fadeout. The text of the save button is "Save as Draft". When clicked I would like it to fade out, change the text to "Draft saved at [time]" and then fade in to show that it was saved, and the time it was saved. After a few seconds I would like that button message to fade out and be replaced by the original "Save as Draft" button.

I'm thinking I have to nest the fades into functions, but my jquery fading talents are minimal and fading rapidly :)

Below is the success part of the ajax ....

success: function(data) {
    var date = new Date(),
    hours = date.getHours(),
    minutes = date.getMinutes();

    if (hours > 12) hours = hours - 12;
    if (minutes < 10) minutes = '0' + minutes;

    var save_draft = '<div id="save-button" class="button-clone">Save as Draft</div>';
    var draft_saved = '<div id="save-button" class="button-clone">Draft Saved at ' + hours + ':' + minutes + '</div>';

    $('#save-button').fadeOut(1000);
    $('#save-button').replaceWith(draft_saved);
    $('#save-button').fadeIn(1000);
    $('#save-button').fadeOut(1000);
    $('#save-button').replaceWith(save_draft);
    $('#save-button').fadeIn(1000);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深巷少女 2024-11-12 19:44:39

由于每个动画都是异步的,因此您需要调用回调函数内的任何其他方法。

编辑

抱歉,它不起作用。也用 fadeIn 进行调整:

$('#save-button').fadeOut("slow", function() {
    $('#save-button').replaceWith(draft_saved);
    $('#save-button').hide().fadeIn(1000, function() {
        $('#save-button').fadeOut(1000, function() {
            $('#save-button').replaceWith(save_draft);
            $('#save-button').hide().fadeIn(1000);
        });
    });
});

让我解释一下为什么需要这个 hide()

当你 fadeOut 某个元素时,jQuery 会放置 display : none 直接指向该 HTML。但是您正在替换为另一个 HTML(通过 replaceWith() 方法)。但是 fadeIn() 只会淡出隐藏的元素。因此,我们需要隐藏新的 HTML,然后 fadeIn() 发生。

Since each animation is async, you need to call any other method inside the callback function.

EDIT

Sorry, it wasn't working. Adjusted with fadeIn too:

$('#save-button').fadeOut("slow", function() {
    $('#save-button').replaceWith(draft_saved);
    $('#save-button').hide().fadeIn(1000, function() {
        $('#save-button').fadeOut(1000, function() {
            $('#save-button').replaceWith(save_draft);
            $('#save-button').hide().fadeIn(1000);
        });
    });
});

Let me explain why this hide() is being needed:

When you fadeOut some element, jQuery puts display: none directly to that HTML. But you is replacing with another HTML (by replaceWith() method). But fadeIn() only fades elements that was hidden. So, we need to hide the new HTML and then the fadeIn() happens.

べ映画 2024-11-12 19:44:39

你说得对。你需要筑巢。尝试这样的事情......

var date = new Date(),
hours = date.getHours(),
minutes = date.getMinutes();

if (hours > 12) hours = hours - 12;
if (minutes < 10) minutes = '0' + minutes;

var save_draft = '<div id="save-button" class="button-clone">Save as Draft</div>';
var draft_saved = '<div id="save-button" class="button-clone">Draft Saved at ' + hours + ':' + minutes + '</div>';

$('body').append(save_draft).;

$('#save-button').fadeOut(1000,function(){
    $('#save-button').replaceWith(draft_saved);
    $('#save-button').fadeIn(1000,function(){
        $('#save-button').fadeOut(1000, function(){
            $('#save-button').replaceWith(save_draft);
            $('#save-button').fadeIn(1000);
        });
    });
});

You're right. You need to nest. Try something like this...

var date = new Date(),
hours = date.getHours(),
minutes = date.getMinutes();

if (hours > 12) hours = hours - 12;
if (minutes < 10) minutes = '0' + minutes;

var save_draft = '<div id="save-button" class="button-clone">Save as Draft</div>';
var draft_saved = '<div id="save-button" class="button-clone">Draft Saved at ' + hours + ':' + minutes + '</div>';

$('body').append(save_draft).;

$('#save-button').fadeOut(1000,function(){
    $('#save-button').replaceWith(draft_saved);
    $('#save-button').fadeIn(1000,function(){
        $('#save-button').fadeOut(1000, function(){
            $('#save-button').replaceWith(save_draft);
            $('#save-button').fadeIn(1000);
        });
    });
});
苯莒 2024-11-12 19:44:39

你应该这样做:

$('#save-button').fadeOut(1000, function(){
$('#save-button').replaceWith(draft_saved, function(){
$('#save-button').fadeIn(1000, function(){
$('#save-button').fadeOut(1000, function(){
$('#save-button').fadeIn(1000);
}););}););}););});

我没有缩进代码,所以它看起来不乱
希望这有帮助。干杯

You should do this:

$('#save-button').fadeOut(1000, function(){
$('#save-button').replaceWith(draft_saved, function(){
$('#save-button').fadeIn(1000, function(){
$('#save-button').fadeOut(1000, function(){
$('#save-button').fadeIn(1000);
}););}););}););});

I didn't indent the code so it doesn't look messy
Hope this helps. Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文