javascript - 如何在 jQuery 和 CSS 中减少我的代码

标签 javascript jquery html css

这是我的代码:

jQuery:

$(document).ready(function () {
        $("#news").hover(function () {
            $('#news_img').animate({
                height: 'toggle'
            }, 290, function () {
            });
        });

        $("#news1").hover(function () {
            $('#news_img1').animate({
                height: 'toggle'
            }, 290, function () {
            });
        });

        $("#news3").hover(function () {
            $('#news_img3').animate({
                height: 'toggle'
            }, 290, function () {
            });
        });

        $("#news4").hover(function () {
            $('#news_img4').animate({
                height: 'toggle'
            }, 290, function () {
            });
        });
    });

JSFIDDLE 在这里: <强> http://jsfiddle.net/huydq91/N89Kw/

我想减少我的代码,以便将来我想添加更多代码时更易于管理 <tr><td>无需在 jQuery 和 CSS 中过多编辑标签。

最佳答案

您可以通过其类 news 定位悬停元素,并通过将悬停元素的 id 中的最后数字附加到 news_img 来找到目标元素,例如

$(document).ready(function () {
    $(".news").hover(function () {
        $('#news_img' + this.id.replace('news', '')).stop(true).animate({
            height: 'toggle'
        }, 290, function () {});
    });
});

演示:Fiddle


您可以通过向图像添加一些 data-* 属性来删除悬停的 css 部分,例如

<img src="http://i853.photobucket.com/albums/ab100/vimeo1903/Showroom1_zps923b43dc.jpg" border="0" alt="Showroom1" data-hover="http://i853.photobucket.com/albums/ab100/vimeo1903/Showroom1_1_zpse41d0851.jpg" />

然后

$(document).ready(function () {
    //since the news elements has a common class, use it to target all the news elements instead of using individual ids
    $(".news").hover(function (e) {
        //you can find the `news_img` within the current news item using .find() instead of using its class to target it
        $(this).find('.news_img').stop(true).animate({
            height: 'toggle'
        }, 290);
        //find the image element within the current news
        var $img = $(this).find('.imgswap img');
        //if the current event is mouseenter then show the hover image else the src image
        //the hover handler registers 2 handler one for mouseenter another for mouseleave
        $img.attr('src', $img.data(e.type == 'mouseenter' ? 'hover' : 'src'));
    });
    //when we leaves the news elements we need to put back the original src, so store it using data api
    $('.news .imgswap img').each(function () {
        $(this).data('src', this.src);
    })
});

关于javascript - 如何在 jQuery 和 CSS 中减少我的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22926913/

相关文章:

javascript - 背景图片幻灯片 |在幻灯片放映开始之前使过渡平滑并预加载图像

javascript - jqgrid + 树网格 + 子网格

javascript - Safari ipad 作为 parseFloat 的结果返回 int

Javascript 安全错误 : DOM Exception 18 while accessing cookies

html - 如何将 4 个 div 组合在一起并并排显示?

javascript - 哪些资源可用于构建 2 人国际象棋游戏界面?

javascript - “历史”、无限制全局变量和 window.history

jquery - 使用 CoffeeScript 过早执行代码

javascript - 函数 Name() 与不带括号的函数名称之间的区别?

javascript - 将 $.ajax() 中的回调响应视为 JSON