简体   繁体   English

我该如何实现这个jquery插件?

[英]How should I implement this jquery plug-in?

I'm using the jquery cycle plug-in to create a picture slideshow. 我正在使用jquery循环插件来创建图片幻灯片。 It was super easy and I understand that code and I've gotten it to work on its own. 这非常容易,我理解代码,我已经让它自己工作了。 But I'm trying to implement into an already existing website and am having some trouble. 但我正在尝试实施已经存在的网站并遇到一些麻烦。 The website is broken up into separate files for the html, css, and then a js file. 该网站分为html,css和js文件的单独文件。

This code is supposed to go within script tags in the head, but for purposes of the site it needs to go in the js file. 这段代码应该放在头部的脚本标签内,但是为了网站的目的,它需要进入js文件。 I guess my question is do I need to do anything to the following code to make it ready to go in the js file, like put it in a function? 我想我的问题是我是否需要对以下代码做任何事情以使其准备好进入js文件,就像把它放在函数中一样? I have tried just this without any luck. 我没试过就试过这个。 (And I have the jquery files loaded already). (我已经加载了jquery文件)。

$('.rotatorA').cycle({ fx: 'fade', timeout: 3000 }); $('。rotatorA')。cycle({fx:'fade',timeout:3000});

$('.rotatorB').cycle({ fx: 'fade', delay: 1000, timeout: 3000 }); $('。rotatorB')。cycle({fx:'fade',delay:1000,timeout:3000});

I'm fairly new to jquery and I haven't found much info on this, so I would really appreciate some help! 我对jquery相当新,我没有找到太多关于此的信息,所以我真的很感激一些帮助! Thanks! 谢谢!

Make sure it's in a document.ready callback, ie: 确保它在document.ready回调中,即:

$(document).ready(function() {
    $('.rotatorA').cycle({ fx: 'fade', timeout: 3000 });
    $('.rotatorB').cycle({ fx: 'fade', delay: 1000, timeout: 3000 }); 
});

But that's needed regardless of whether it was in the <head> tag or in an external script...I mention it because maybe you left it out when copying the script? 但无论是在<head>标签还是在外部脚本中,都需要它......我提到它是因为你可能在复制脚本时将其遗漏了?

FYI $(function() is available as an abbreviated version of $(document).ready(function() FYI $(function()可作为$(document).ready(function()的缩写版本$(document).ready(function()

Also make sure you don't have any other libraries like MooTools loaded. 还要确保没有任何其他库,如MooTools加载。 If that's the case then you need to modify your code somewhat and add a script with jQuery.noConflict() right after loading jQuery. 如果是这种情况,则需要稍微修改代码并在加载jQuery后jQuery.noConflict()添加带有jQuery.noConflict()的脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM