【问题标题】:jQuery - Replacing image sources dynamically for mouse eventsjQuery - 为鼠标事件动态替换图像源
【发布时间】:2012-12-26 08:15:02
【问题描述】:

我有一个示例导航列表,其中包含图像作为按钮。当用户将鼠标悬停在按钮上和单击鼠标时,按钮有两种附加状态。每个按钮都有相同的命名约定,因此对于后退按钮将有“back.jpg”、“back_over.jpg”和“back_down.jpg”。由于一个页面上会有几个按钮,我的目标是获取所选图像源的路径,然后通过附加/删除路径的适当部分来修改它。

我有一个工作示例,但我觉得它可以更优雅。例如,我很确定可以将 3 个图像路径中的每一个存储为变量并以某种方式传递它们。但是当我尝试这种方法时,img src 不会正确附加或删除。我也知道这可以通过 CSS 实现,我已经做到了。这主要是为了增加我对jQuery的理解。

    $(document).ready(function() {

        $('li > img').bind('mouseenter mousedown mouseup mouseleave click', function(event) {

            var overImgSrc = $(this).attr('src').match(/[^\.]+/) + '_over.jpg';
            var downImgSrc = $(this).attr('src').replace('_over.jpg', '_down.jpg');
            var upImgSrc = $(this).attr('src').replace('_down.jpg', '_over.jpg');
            var outImgSrc = $(this).attr('src').replace('_over.jpg', '.jpg');

            var evtType = event.type;

            switch (evtType) {
                case 'mouseenter':
                $(this).attr('src', overImgSrc);
                break;

                case 'mousedown':
                $(this).attr('src', downImgSrc);
                break;

                case 'mouseup':
                $(this).attr('src', upImgSrc);
                break;

                case 'mouseleave':
                    if ($(this).attr('src', downImgSrc)) {
                        var downOutImgSrc = $(this).attr('src').replace('_down.jpg', '.jpg');
                        $(this).attr('src', downOutImgSrc);
                    }
                    else {
                        $(this).attr('src', outImgSrc);
                    }
                break;

                case 'click':
                alert("Yowza!");
                break;

                default:
                break;
            }
        });
    });

HTML

<ul id="nav">
        <li><img src="images/Back.jpg"></li>
        <li><img src="images/Next.jpg"></li>
</ul>

【问题讨论】:

  • 您的 if 语句正在设置 src if ($(this).attr('src', downImgSrc))
  • 顺便说一句,mouseleave 案例中的额外位是为了防止在用户单击按钮然后在释放之前将鼠标拖出时不正确附加 iamge src。

标签: jquery replace this image src


【解决方案1】:

试试这个:

$(document).ready(function() {
    $('li > img').bind('mouseenter mousedown mouseup mouseleave click', function(event) {
        var images = {
            'mouseenter' : '_over.jpg',
            'mousedown'  : '_down.jpg',
            'mouseup'    : '_over.jpg',
            'mouseleave' : '.jpg',
        };

        //Store the original source without the extension.
        if (this.originalSource == undefined) {
            this.originalSource = this.src.substring(0, this.src.lastIndexOf('.'));
        }

        var evtType = event.type;

        //Append the affector.
        if (evtType in images) {
            this.src = this.originalSource + images[evtType];
        }

        //Handle your click event. (left the switch event in case you want to do something else with other events)
        switch (evtType) {
        case 'click':
            alert("Yowza!");
            break;
        default:
        }
    });
});

【讨论】:

  • 我喜欢它,而且我从来没有想过要以这种方式尝试它。非常感谢!
【解决方案2】:

将图像存储为数组,并通过索引引用它们。

var $images = ['image1.jpg','image2.jpg'];

case 'mouseenter':
    $(this).attr('src', $images[0]);
    break;

粗略的例子,但你明白了。

edit:因为应用不太可行,所以去掉了动态创建。

【讨论】:

  • src 会因下一个事件而改变,所以他最终会在这种情况下一遍又一遍地附加 '_over.jpg'。
  • 他在做一个字符串替换,而不是一个追加......如果没有匹配,它就会把它留在_over
  • 谢谢。我真正想要的是更多地了解如何操作 img src 路径。我觉得一些不同的视角会增加我对jQuery的理解。
猜你喜欢
  • 2012-06-23
  • 2014-02-10
  • 2014-05-03
  • 1970-01-01
  • 2018-03-27
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 2011-07-11
相关资源
最近更新 更多