javascript - 从表格布局中由 javascript 运行的动画(悬停时)创建链接

标签 javascript html css animation hover

我有以下代码,一个简单的三列布局,每列都有一个悬停时运行的动画。

动画效果很好,但我无法将动画按钮设为链接。它以图像作为链接,如果我更改 href,它会失败(这是有道理的)但我不知道在哪里添加链接。

<table style="width: 100%;" border="0">
<tbody>
                <tr>
                    <td>
                    <a href="http://www.gloshospitals.nhs.uk/templates/standard/images/paeds/for-children-anim.gif" target="_blank">
                    <img src="http://www.gloshospitals.nhs.uk/templates/standard/images/paeds/for-children.gif" 
                    data-orig="http://www.gloshospitals.nhs.uk/templates/standard/images/paeds/for-children.gif" width="323" height="323"></a>                      
                    </td>


                    <td>
                    <a href="http://www.gloshospitals.nhs.uk/templates/standard/images/paeds/for-teenagers-anim.gif" target="_blank"><img src="http://www.gloshospitals.nhs.uk/templates/standard/images/paeds/for-teenagers.gif" 
                    data-orig="http://www.gloshospitals.nhs.uk/templates/standard/images/paeds/for-teenagers.gif" width="323" height="323"></a>
                    </td>


                    <td>
                    <a href="http://www.gloshospitals.nhs.uk/templates/standard/images/paeds/for-parents-anim.gif" target="_blank"><img src="http://www.gloshospitals.nhs.uk/templates/standard/images/paeds/for-parents.gif" 
                    data-orig="http://www.gloshospitals.nhs.uk/templates/standard/images/paeds/for-parents.gif" width="323" height="323"></a>
                    </td>
                </tr>
            </tbody>
        </table>

通过以下 javascript 运行。

<script type="text/javascript">
$(function(){
  $('table a img').hover(function(){
    // on mouse enter
    var customdata = $(this).parent().attr('href');
    $(this).attr('src',customdata); 
  }, function(){
    // on mouse leave
    $(this).attr('src',$(this).attr('data-orig'));
  });

});
</script>

在此处的页面 Link to homepage of site.

最佳答案

您的问题令人困惑。我不确定您真正想要的链接是什么,也不确定为什么您当前的实现是错误的。我假设您不希望图像链接作为实际链接。如果这个假设是正确的,这就是你的答案......

<table style="width: 100%;" border="0">
<tbody>
    <tr>
       <td>
           <a href="[Your Link Goes Here]" target="_blank">
           <img 
src="http://www.gloshospitals.nhs.uk/templates/standard/images/paeds/for-children.gif"  
data-img="http://www.gloshospitals.nhs.uk/templates/standard/images/paeds/for-children.gif"
data-animation="http://www.gloshospitals.nhs.uk/templates/standard/images/paeds/for-children-anim.gif" 
width="323" height="323"></a>                      
                    </td>

                </tr>
            </tbody>
        </table>

<script type="text/javascript">
$(function(){
  $('table a img').hover(function(){
    var element = $(this);
    var animateUrl = element.data('animation');
    element.attr('src', animateUrl);  
  }, function(){
    var element = $(this);
    var staticImage = element.data('img');
    element.attr('src', staticImage); 
  });

});
</script>

关于javascript - 从表格布局中由 javascript 运行的动画(悬停时)创建链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42005559/

相关文章:

javascript - Backbone - 当用户通过特定帖子访问网站时如何管理集合初始化?

html - 两根柱子的高度与最深的柱子相同吗?

html - 如何修复两个独立类的 flex 间距

html - 如果前一个列表项比它高,如何在列表项中居中对齐文本

javascript - 在 BootStrap 模态对话框中包含 HTML 代码

javascript - 从多个数组中提取随机项

javascript - 如何在 JavaScript 中有效地合并两个对象数组并按属性过滤重复项?

php - 为什么我的导航链接仍然带有下划线?即使有 !important 声明

html - 当悬停时填充扩展时,div 向下移动

html - 无法让 select2 保管箱在列表框行中工作