简体   繁体   English

如何在 JavaScript 中调用 jquery 函数?

[英]How to call the jquery function in JavaScript?

I am trying to call the jquery flip function in my javascript code, but i am not able to do that.我试图在我的 javascript 代码中调用 jquery 翻转函数,但我无法做到这一点。

Code:代码:

var footwear = [
        ['images/product_images/footwear/footwear_1.jpeg'],
        ['images/product_images/footwear/footwear_2.jpeg'],
        ['images/product_images/footwear/footwear_3.jpeg'],
        ['images/product_images/footwear/footwear_4.jpeg'],
        []
];

function startSlidecat1() {
  for (var i = 0; i < footwear.length; i++) {
    var image = footwear[i][0];
    imgslidercat1(image, i * 2100, i == footwear.length - 1);
  }
};

function imgslidercat1(image, timeout, last) {
  window.setTimeout(function() {
    document.getElementById('category-1').innerHTML = "";
    var product = document.getElementById('category-1');
    var elem = document.createElement("img");
    product.appendChild(elem);
    elem.src = image;
    if (last) {
      startSlidecat1();
    }
  }, timeout);
}
startSlidecat1();

Jquery flip code jQuery 翻转代码

$(document).ready(function () {
    $('.box-1').delay(400).css('display', 'block');
    $('.box-1').transition({
        perspective: '100px',
        rotateY: '360deg'
    }, 600)
});

The javascript code iterates through array and calls the imgslidercat1() function with parameters, and at end the function is repeated execution. javascript 代码遍历数组并调用带有参数的imgslidercat1()函数,最后重复执行该函数。 but before repeating execution i want to call JQuery flip action code.但在重复执行之前,我想调用 JQuery 翻转动作代码。 the jquery code sets display to block at beginning, at the end it should set display back to none. jquery 代码在开始时将显示设置为阻止,最后它应该将显示设置回无。 after executing the jquery the javascript should continue again.执行 jquery 后,javascript 应该再次继续。

How can i do this?我怎样才能做到这一点?

Separate this into another function把它分成另一个函数

function flip(){
   $('.box-1').delay(400).css('display', 'block');
    $('.box-1').transition({
        perspective: '100px',
        rotateY: '360deg'
    }, 600)

}

Then call it from where you need it然后从你需要的地方调用它

$(document).ready(function () {
  flip();
}

if (last) {
  flip();
}

First, you need to place that code inside a new function so that it can be used outside of the document ready block.首先,您需要将该代码放在一个新函数中,以便它可以在文档就绪块之外使用。

Also, because you use .delay() and .transition() , it may take some time for the animation to be done, so you should work with callbacks to continue the loop.此外,因为您使用.delay().transition()它可能需要一些时间做动画,所以你应该有回调的工作,继续循环。

A good approach would be to encapsulate the process into a closure like this:一个好的方法是将过程封装成这样的闭包:

 var footwear = [ ['images/product_images/footwear/footwear_1.jpeg'], ['images/product_images/footwear/footwear_2.jpeg'], ['images/product_images/footwear/footwear_3.jpeg'], ['images/product_images/footwear/footwear_4.jpeg'], [] ]; var slider = function(footwear, flip, delay) { var i = 0; function slide(image) { // handle each image $('#category-1').html($('<img>', {src: image})); flip(next); // animate and queue next image } function next() { i = (i + 1) % footwear.length; setTimeout(process, delay); // schedule next process } function process() { slide(footwear[i]); } return { start: function() { process(); } } }(footwear, flip, 2100); function flip(callback) { $('.box-1') .delay(400) .css('display', 'block') .transition({ perspective: '100px', rotateY: '360deg' }, 600, callback); } $(document).ready(function () { // kick off the slider slider.start(); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://ricostacruz.com/jquery.transit/jquery.transit.js"></script> <div id="category-1" class="box-1" />

Move the anonymous function into a named function like so像这样将匿名函数移动到命名函数中

Also cache your jQuery objects还缓存您的 jQuery 对象

var box = $('.box-1');
function flip() {
    box.delay(400).css('display', 'block');
    box.transition({
        perspective: '100px',
        rotateY: '360deg'
    }, 600);
}

Then call this function in the following places.然后在以下地方调用这个函数。

function imgslidercat1(image, timeout, last) {
  window.setTimeout(function() {
    document.getElementById('category-1').innerHTML = "";
    var product = document.getElementById('category-1');
    var elem = document.createElement("img");
    product.appendChild(elem);
    elem.src = image;
    if (last) {
      flip(); // Call it here
      startSlidecat1();
    }
  }, timeout);
}
$(document).ready(flip); // Call it here

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

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