jQuery - 如何将字符串作为参数传递给动画函数
我正在尝试以字符串形式检索参数,并将它们直接提供给 jQuery 的动画函数,如下所示:
my html:
<img src="myImage.png" rel="{top:'20px',left:'1000px'},500|{top:'20px',left:'10px'},500" class="slide-content" />
my jQuery code:
var $elem = $('.slide-content');
var settings = $elem.attr('rel').split('|');
$elem.animate(settings[1]);
这不起作用。如果我手动设置它,它就可以工作:
$elem.animate({top:'20px',left:'1000px'},500);
是否有一种简单的方法可以使其工作?或者我是否必须解析字符串并构建一个设置对象?
任何指示将不胜感激,谢谢!
根据乔恩的回答进行编辑: 结果你必须解析正确形成的 JSON,所以我更新了代码,如下
HTML:
<img src="myImage.png" rel='{"anim":{"top":"20px","left":"1000px"},"speed":"500"}|{"anim":{"top":"20px","left":"20px"},"speed":"500"}' class="slide-content" />
注意 html rel 属性是如何用单引号而不是双引号括起来的。这是因为 JSON 元素需要用双引号引起来。
jQuery 部分:
var $elem = $('.slide-content');
var settings = $elem.attr('rel').split('|');
var animIn = $.parseJSON(settings[1]);
var animOut = $.parseJSON(settings[0]);
$elem.animate(animIn.anim, animIn.speed).delay(2000).animate(animOut.anim, animIn.speed);
只是为了澄清问题背后的原因:
这将作为滑块插件的基础,其中每个内部部分将独立进行动画处理,并且必须易于用户在 HTML 元素本身内进行设置
必须在使用 XHTML 的网站上运行
无论如何,感谢您的快速回复和有用的指示:)
I'm trying to retrieve parameters as a string and directly feed them to jQuery's animate function like so:
my html:
<img src="myImage.png" rel="{top:'20px',left:'1000px'},500|{top:'20px',left:'10px'},500" class="slide-content" />
my jQuery code:
var $elem = $('.slide-content');
var settings = $elem.attr('rel').split('|');
$elem.animate(settings[1]);
This does not work. If I put the setting manually it works:
$elem.animate({top:'20px',left:'1000px'},500);
Is there a simple way of making it work? Or do I have to parse the string and build up a settings object?
Any pointers would be appreciated, thanx!
Edit after Jon's anwser:
Turns out you have to parse correctly formed JSON and so I updated the code like so
HTML:
<img src="myImage.png" rel='{"anim":{"top":"20px","left":"1000px"},"speed":"500"}|{"anim":{"top":"20px","left":"20px"},"speed":"500"}' class="slide-content" />
Notice how the html rel attribute is wrapped in single quotes instead of double quotes. This is because JSON elements need to be wrapped in double quotes.
jQuery part:
var $elem = $('.slide-content');
var settings = $elem.attr('rel').split('|');
var animIn = $.parseJSON(settings[1]);
var animOut = $.parseJSON(settings[0]);
$elem.animate(animIn.anim, animIn.speed).delay(2000).animate(animOut.anim, animIn.speed);
Just to clear up the why behind the question:
this will serve as basis for a slider plugin in which each inner part will be animated independently and must be easy for the user to setup within the HTML element itself
must run on a website using XHTML
Anyhow thank you for the quick responses and helpfull indications :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要传递一个对象,而不是字符串。您已经有了对象的 JSON 表示形式,因此您只需要从中创建一个对象即可。使用
JSON.parse
来做到这一点,所有现代浏览器都支持。更新:我只是更加注意并意识到您也尝试在其中的动画持续时间中进行硬拉,但这没有帮助(它使事情变得更加复杂)。您应该考虑将 HTML 更改为类似:
这样您就可以执行以下操作:
顺便说一句:像这样征用
rel
属性可能不是最好的主意。如果您想将任意数据附加到这样的元素,您可以使用 HTML 5 数据属性:You need to pass an object, not a string. You already have the JSON representation of an object, so you just need to make an object out of it. Do that with
JSON.parse
, which all modern browsers support.Update: I just paid more attention and realized that you have tried to also shoehorn in the animation duration in there, which doesn't help (it makes things more complicated). You should consider changing the HTML to something like:
which would then allow you to do:
As an aside: commandeering the
rel
attribute like that is probably not the best of ideas. If you want to attach arbitrary data to an element like that, you can use HTML 5 data attributes:我认为您忘记从 rel 属性值中解析 json 字符串。
var settings = $.parseJSON($(elem).attr('rel').split('|'));
I think you forgot to parse the json String out of the rel attribute value.
var settings = $.parseJSON($(elem).attr('rel').split('|'));
您不必编辑任何内容,尝试:
然后您只需
You don't have to edit anything, try:
and then you just