【问题标题】:jQuery - do function THEN initiate mailtojQuery - 做功能然后启动邮件
【发布时间】:2014-08-23 00:59:26
【问题描述】:

谁能解释一下?

我正在构建一个顶部有一条小丝带的网站,用于启动 mailto。单击功能区时,我希望它弹跳。麻烦的是,mailto: 同时触发,并且由于电子邮件客户端窗口出现在站点上而错过了功能区的反弹效果。

有没有办法使用 jQuery 延迟 mailto 操作 - 或通过 jQuery 添加 mailto?

到目前为止,我已经完成了以下操作……我已经阻止了默认操作,并且反弹正在工作 - 我只需要再次触发 mailto 的最后一点。

提前谢谢你……

$(document).ready(function() {         
   $( "a#pullTag" ).click(function( event ) {
      event.preventDefault();
      doBounce($(this), 3, '10px', 100);   
   });
   function doBounce(element, times, distance, speed) {
      for(i = 0; i < times; i++) {
      element.animate({marginTop: '-='+distance},speed)
      .animate({marginTop: '+='+distance},speed);
      }        
   } 
});

HTML 很简单:

 <a href="#" id="pullTag">Email Me</a>

【问题讨论】:

  • 你好,mailTo 在哪里?
  • 以上编辑 - 谢谢
  • +10 来自我,享受先生 :)

标签: jquery mailto


【解决方案1】:

尝试使用(通过 jQuery 触发 mailto 事件):

window.location.href = "mailto:address@dmail.com";

所以在你的代码中它可能看起来像:

$(document).ready(function() {         
   $( "a#pullTag" ).click(function( event ) {
      event.preventDefault();
      doBounce($(this), 3, '10px', 100);   
      window.location.href = "mailto:address@dmail.com";
   });
   function doBounce(element, times, distance, speed) {
      for(i = 0; i < times; i++) {
      element.animate({marginTop: '-='+distance},speed)
      .animate({marginTop: '+='+distance},speed);
      }        
   } 
});

JSFiddle here

【讨论】:

  • 你能给jsfiddle吗?
  • 这将在动画仍在播放甚至刚刚开始时立即将窗口触发到mailto 地址?!
  • @RobSchmuecker 这将调用doBounce() 函数,然后触发mailto 事件
  • doBounce 将使动画排队,但不会等到它们完成。 mailto 将在第一个动画结束之前被调用。
  • @pes502 使用 chrome dev-tools 或 firebug 之类的 web-inspector 控制台打开我在这里修改过的小提琴,看看我的意思。 jsfiddle.net/KmU3Y/1
【解决方案2】:

你可以这样做:

$(document).ready(function () {
    $("a#pullTag").click(function (event) {
        event.preventDefault();
        doBounce($(this), 3, '10px', 100);
    });

    function doBounce(element, times, distance, speed) {
        for (i = 0; i < times; i++) {
            element.animate({ marginTop: '-=' + distance }, speed)
                   /*
                   I have added a callback function to the last .animate()
                   This function checks the animation queue length
                   When the last animation is reached (queue length == 1),
                   it changes the current URL to the link's [href] attribute
                   */
                   .animate({ marginTop: '+=' + distance }, speed, function() {
                        if (element.queue().length <= 1) {
                            window.location = element.prop('href');
                        }
                    });
        }
    }
});

You can view a JSFiddle demo here

【讨论】:

  • +1 我做过类似的事情,会发帖供参考。
  • 应该是“获胜”的答案
  • 抱歉 - 以为我将其标记为“有帮助”而不是“获胜”答案
  • 而这就是帮助 - 将 document.location 更改为 window.location.href = "mailto:xxx@xxx";它工作得很好。谢谢。
【解决方案3】:

使用对最后一个动画函数的回调,您可以修改 doBounce 以在所有动画完成时调用。

Javascript:

$(document).ready(function () {
    $("a#pullTag").click(function (event) {
        event.preventDefault();
        var href = $(this).attr('href');
        doBounce($(this), 3, '10px', 1000, href);
    });

    function doBounce(element, times, distance, speed) {
        element.animate({
            marginTop: '-=' + distance
        }, speed)
            .animate({
            marginTop: '+=' + distance
        }, speed, 'swing', function () {
            times = times - 1;
            if (times === 0) {
                console.log('animation complete, redirecting to href');
                window.location.href = element.attr('href');
            } else {
                console.log('animation not yet complete');
                doBounce(element, times, distance, speed);
            }
        });
    }
});

在这里演示http://jsfiddle.net/hPWMw/1/

【讨论】:

  • +1 :同样的想法,比接受的答案更好,因为它实际上是在等待动画结束。
  • 耶!我同意,接受的答案并不比原来的困境好?!
猜你喜欢
  • 2013-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-22
  • 2012-12-28
  • 1970-01-01
  • 2021-03-01
相关资源
最近更新 更多