javascript - jQuery 用动画改变图像并等待它触发超链接

标签 javascript jquery html animation

我想使用 url attr 在 .click() 上切换图像我可以这样做,但我不知道如何让它等待,直到动画完成重定向到新网页。 这是js

$(function() {
    $('.cv').click(function() {
        $("#cv img").fadeOut(2000, function() {
            $(this).load(function() {
                $(this).fadeIn();
            });
            $(this).attr("src", "images/cv2.png");
            return true;
        });
    });
}); 

这是 html:

<div id="cv" class="img one-third column">
  <a class="cv" target="#" href="cv.joanlascano.com.ar">
  <img src="images/cv1.png" alt="Curriculum"/>
  <br />CV</a>
</div>

最佳答案

您需要通过从点击处理程序返回 false 来防止浏览器默认跟随链接。然后在 fadeIn() 的回调中做重定向

$(function() {
    $('.cv').click(function() {
        /* store url*/
        var href = this.href;
        $("#cv img").fadeOut(2000, function() {
            /* bind load to image*/
            $(this).load(function() {
                $(this).fadeIn(function() {
                    /* redirect in callback of fadeIN*/
                    window.location = href;
                });
                /* change src*/
            }).attr("src", "images/cv2.png");
        });
        /* prevent browser default following href*/
        return false;
    });
});

关于javascript - jQuery 用动画改变图像并等待它触发超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11166508/

相关文章:

html - 在 CSS/HTML 中重新居中网页

html - 具有 2 列和不同高度的水平卡片

JavaScript - 将偶数索引数组元素乘以 2 并将奇数索引数组元素乘以 3

javascript - 检查对象是否包含具有值的属性

javascript - 不使用当前分配给公共(public)函数的对象数据的调用方法

javascript - 在 jQuery 布局插件上调整大小时,放置在调整器上的按钮消失

javascript - 多次调用 .typed() 函数

javascript - 表格单元格悬停和行跨度

javascript - 获取 li 元素的文本

javascript - Node.js:console.err() 是同步还是异步?