ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript-如何调用jquery插件中的此函数?

2019-12-09 23:35:32  阅读:221  来源: 互联网

标签:jquery-plugins javascript jquery


我在页面vTicker上使用了一个jquery插件,“用于轻松简单地进行垂直新闻自动滚动”.我将它与rss jquery插件结合使用.它工作正常,但是我需要创建一个可以手动滚动的按钮.谁能告诉我该怎么做?我猜想我需要从vTicker文件中调用moveUp函数,但是由于该函数的创建方式以及vticker本身的创建方式,我不确定该怎么做.

我这样创建我的vTicker:

$('#ticker1').rssfeed(uRL).ajaxStop(function() {
$('#ticker1 div.rssBody').vTicker();
})

这是vTicker代码:

/*
* Tadas Juozapaitis ( kasp3rito@gmail.com )
*/
(function($){
$.fn.vTicker = function(options) {
    var defaults = {
        speed: 700,
        pause: 15000,
        showItems: 3,
        animation: '',
        mousePause: true,
        isPaused: false
    };

    var options = $.extend(defaults, options);


    moveUp = function(obj2, height){


        if(options.isPaused)
            return;

        var obj = obj2.children('ul');

        var iframe = $('#iFrame2');


        first = obj.children('li:first').clone(true);
        second = obj.children('li:odd:first').clone(true);

        iframe.attr('src', (second.children('h4').children('a').attr("href")));


        obj.animate({top: '-=' + height + 'px'}, options.speed, function() {
            $(this).children('li:first').remove();
            $(this).css('top', '0px');
        });

        if(options.animation == 'fade')
        {
            obj.children('li:first').fadeOut(options.speed);
            obj.children('li:last').hide().fadeIn(options.speed);
        }

        first.appendTo(obj);
    };

    return this.each(function() {
        var obj = $(this);
        var maxHeight = 0;

        obj.css({overflow: 'hidden', position: 'relative'})
            .children('ul').css({position: 'absolute', margin: 0, padding: 0})
            .children('li').css({margin: 0, padding: 0});

        obj.children('ul').children('li').each(function(){
            if($(this).height() > maxHeight)
            {
                maxHeight = $(this).height();
            }
        });

        obj.children('ul').children('li').each(function(){
            $(this).height(maxHeight);
        });

        obj.height(maxHeight * options.showItems);

        var interval = setInterval(function(){ moveUp(obj, maxHeight); }, options.pause);

        if(options.mousePause)
        {
            obj.bind("mouseenter",function(){
                options.isPaused = true;
            }).bind("mouseleave",function(){
                options.isPaused = false;
            });
        }
    });
};
})(jQuery);

谢谢阅读.

解决方法:

简短的答案是,你不能. moveUp函数在插件范围内是完全隔离的,您不能直接调用它.

要修改插件,以便您可以手动滚动,请在该行返回this.each(function(){:

$.fn.extend({
  vTickerMoveUp: function() {
    var obj = $(this);
    var maxHeight = 0;
    obj.children('ul').children('li').each(function(){
        if($(this).height() > maxHeight) maxHeight = $(this).height();
    });
    moveUp(obj, maxHeight);
  }
});

然后,要滚动,请执行以下操作:

var ticker = $('#ticker1 div.rssBody').vTicker();
ticker.vTickerMoveUp();

标签:jquery-plugins,javascript,jquery
来源: https://codeday.me/bug/20191209/2097882.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有