jQuery - 在动画 div 内的图像源之间动态交替

发布于 2024-10-18 10:52:57 字数 857 浏览 5 评论 0原文

我有一个动画菜单,当通过单击箭头图像触发时可以在页面上滑动。我想在菜单动画完成后将箭头切换到不同的图像源,然后在菜单关闭时返回到原始文件。

最好的方法来做到这一点? 谢谢你!

HTML:

<div id="slider">
     <div id="trigger_right">
          <img class="arrow_small" src="images/left_small.png" alt="slide menu out" />
     </div>
     <div class="trans" id="overlay"></div>
     <div id="content">
          <p>This is content</p>
     </div>
</div>

JavaScript:

$(function() {
    $('#trigger_right').toggle(function (){
        $('#slider').animate({'width':'100%'}, 1500);
            $('.arrow_small').attr('src','images/right_small.png');
    }, function() {
        $('#slider').animate({'width':'30px'}, 1500);
            $('.arrow_small').attr('src','images/left_small.png');
    });
});

I have a menu that is animated to slide across the page when triggered by clicking on an image of an arrow. I'd like to switch the arrow to a different image source once the menu's animation has completed, and then return back to the original file when the menu has been closed.

Best way to do this?
Thank you!

HTML:

<div id="slider">
     <div id="trigger_right">
          <img class="arrow_small" src="images/left_small.png" alt="slide menu out" />
     </div>
     <div class="trans" id="overlay"></div>
     <div id="content">
          <p>This is content</p>
     </div>
</div>

Javascript:

$(function() {
    $('#trigger_right').toggle(function (){
        $('#slider').animate({'width':'100%'}, 1500);
            $('.arrow_small').attr('src','images/right_small.png');
    }, function() {
        $('#slider').animate({'width':'30px'}, 1500);
            $('.arrow_small').attr('src','images/left_small.png');
    });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只涨不跌 2024-10-25 10:52:57

jQuery 的 .animate 有一个回调,当动画完成时会调用该回调,因此您可以使用它在适当的时间更改图像:

$(function() {
    $('#trigger_right').toggle(function () {
        $('#slider').animate({'width':'100%'}, 1500, function() {
            $('.arrow_small').attr('src','images/right_small.png');
        });
    }, function() {
        $('#slider').animate({'width':'30px'}, 1500, function() {
            $('.arrow_small').attr('src','images/left_small.png');
        });
    });
});

上面假设您只有一个 .arrow_small 元素。使用箭头类和图像精灵表会更好,但这只需要将 $('.arrow_small').attr() 部分更改为 $('. arrow_small').toggleClass() 按照 Rob 的建议进行调用。

jQuery's .animate has a callback that is called when the animate is finished so you can use that to change the images at the appropriate time:

$(function() {
    $('#trigger_right').toggle(function () {
        $('#slider').animate({'width':'100%'}, 1500, function() {
            $('.arrow_small').attr('src','images/right_small.png');
        });
    }, function() {
        $('#slider').animate({'width':'30px'}, 1500, function() {
            $('.arrow_small').attr('src','images/left_small.png');
        });
    });
});

The above assumes that you only have one .arrow_small element of course. Using a class for the arrow and a sprite sheet for the images would be better but that would only require changing the $('.arrow_small').attr() parts to $('.arrow_small').toggleClass() calls as Rob suggests.

ヤ经典坏疍 2024-10-25 10:52:57

如果我理解正确的话,您只想在菜单动画完成后更改图像。

一种方法(也许不是最好的方法)是使用 setTimeout() 在一段设定的时间后发生更改 src 属性的 JavaScript。而不是:

$('.arrow_small').attr('src','images/right_small.png');

你会:

setTimeout("toggleImages()", 1500);

function toggleImages(){
    // some code to toggle them
}

我还没有测试过这个,但试试吧。希望有帮助!

If I understand correctly, you only want the images to change after the menu animation has completed.

One way, perhaps not the best, would be to make the JavaScript that changes the src attribute occur after a set period of time using setTimeout(). Instead of:

$('.arrow_small').attr('src','images/right_small.png');

You would have:

setTimeout("toggleImages()", 1500);

function toggleImages(){
    // some code to toggle them
}

I haven't tested this, but give it a try. Hope it helps!

乜一 2024-10-25 10:52:57

我建议你在 CSS 中将图像设置为类,然后执行以下操作:

.toggleClass("right-small left-small");

I would suggest you set the images up in CSS as classes and then do something like:

.toggleClass("right-small left-small");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文