jQuery - 在动画 div 内的图像源之间动态交替
我有一个动画菜单,当通过单击箭头图像触发时可以在页面上滑动。我想在菜单动画完成后将箭头切换到不同的图像源,然后在菜单关闭时返回到原始文件。
最好的方法来做到这一点? 谢谢你!
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jQuery 的
.animate
有一个回调,当动画完成时会调用该回调,因此您可以使用它在适当的时间更改图像:上面假设您只有一个
.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: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.如果我理解正确的话,您只想在菜单动画完成后更改图像。
一种方法(也许不是最好的方法)是使用 setTimeout() 在一段设定的时间后发生更改 src 属性的 JavaScript。而不是:
你会:
我还没有测试过这个,但试试吧。希望有帮助!
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:
You would have:
I haven't tested this, but give it a try. Hope it helps!
我建议你在 CSS 中将图像设置为类,然后执行以下操作:
I would suggest you set the images up in CSS as classes and then do something like: