简体   繁体   English

使JQuery自动滚动

[英]Make JQuery auto scroll

I have set the following HTML: 我设置了以下HTML:

    <div id="minifrontpage-444" class="minifrontpage-tabbed-fade-in-out">
    <div class="minifrontpageid" style="table-layout: fixed; width: 100%; padding: 0px ! important; margin: 0px ! important; height: 154px;">
        <div class="anim">
        <div style="position:relative;" class="anim-div">
                                <div style="padding: 0px ! important; margin: 0px ! important; display: table; width: 100%; position: absolute;" class="animate activex">
                    <div style="display:table-row;width:100%" class="mfp-table-row">
                            <div style="display:table-cell;width:100%;word-wrap:break-word !important" class="mfp-table-cell">
                    <div class="mfp-table-cell-inner">
                    <h2><a href="/devfwdp.co.uk/index.php?option=com_content&amp;view=article&amp;id=42:lorem-ipsum&amp;catid=13:news&amp;Itemid=101">Lorem ipsum</a></h2><br><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et fermentum massa.
    </p><div class="clrfix"></div>              </div>
                </div>
                        </div>

            </div>                          <div style="padding: 0px ! important; margin: 0px ! important; display: none; width: 100%; position: absolute;" class="animate">
                    <div style="display:table-row;width:100%" class="mfp-table-row">
                            <div style="display:table-cell;width:100%;word-wrap:break-word !important" class="mfp-table-cell">
                    <div class="mfp-table-cell-inner">
                    <h2><a href="/devfwdp.co.uk/index.php?option=com_content&amp;view=article&amp;id=43:lorem-ipsum-2&amp;catid=13:news&amp;Itemid=101">Lorem ipsum (2)</a></h2><br><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et fermentum massa.
    </p><div class="clrfix"></div>              </div>
                </div>
                        </div>

            </div>          </div>
        </div>
    </div>
    <div id="mfptabs"><ul><li class="activex"><span>1</span></li><li><span>2</span></li></ul><div class="clr">&nbsp;</div></div>
    </div>

This is a news scroller, with a 'Next' and 'Previous' button. 这是一个新闻滚动条,带有“下一个”和“上一个”按钮。 I want to be able to have the news scroller rotate automatically, where as currently it's only manually when the buttons are pressed. 我希望能够使新闻滚动条自动旋转,而当前仅在按下按钮时才手动旋转。

I've got the following jQuery, which works great: 我有以下jQuery,效果很好:

    jQuery(document).ready(function(){
        jQuery('.minifrontpage-tabbed-fade-in-out').each(function(){
            var theId = jQuery(this).attr('id');

            var mfpElement = jQuery('#'+theId+' .animate');
            var mfpElementLength = mfpElement.length;

            if(mfpElementLength>1){
                var addto = '<ul>';
                for(var i=0;i<mfpElementLength;i++){
                    addto += '<li><span>'+ (i+1) + '</span></li>';
                }
                addto += '</ul>';
                addto += '<div class="clr">&nbsp;</div>';
                jQuery(addto).appendTo('#'+theId+' #mfptabs');
            }

            mfpElement.hide();
            jQuery('#'+theId+' .animate:first').show();
            jQuery('#'+theId+' .animate:first').addClass('activex');
            jQuery('#'+theId+' #mfptabs li:first').addClass('activex');
            mfpElement.css('position','absolute');
            //mfpElement.css('width',mpfAnimateDiv+'%');
            jQuery('#'+theId+' .minifrontpageid').css('height', jQuery('.animate:first').css('height'));

            jQuery('#'+theId+' #mfptabs li').live('click', function(){ 
                var current = jQuery('#'+theId+' #mfptabs li.activex').index();
                var index = jQuery(this).index();
                if( current != index ){
                    jQuery('#'+theId+' #mfptabs li.activex').removeClass('activex');
                    jQuery(this).addClass('activex'); 

                    jQuery('#'+theId+' .minifrontpageid').animate({
                        height: jQuery(mfpElement[index]).css('height')
                    },
                    {
                        step: function(now, fx) {
                            jQuery(mfpElement[current]).fadeOut();
                            jQuery(mfpElement[index]).fadeIn();
                        }
                    });
                }
                return false;
            });
        });
        });

I am trying to make it autoscroll, so I have tried using the following code, but it's not working: 我正在尝试使其自动滚动,因此我尝试使用以下代码,但无法正常工作:

    function tick(){
        $('.animate:first').slideUp( function () { $(this).appendTo($('.animate')).slideDown(); });
    }
    setInterval(function(){ tick () }, 500);

How can I set it to automatically scroll on page load? 如何设置它以在页面加载时自动滚动?

Solve this using example. 使用示例解决此问题。 In my job i use only 3 slides. 在我的工作中,我只使用3张幻灯片。 I created one function to help and call in trigger: 我创建了一个函数来帮助和调用触发器:

jQuery(document).ready(function(){
jQuery('.minifrontpage-tabbed-fade-in-out').each(function(){
    var theId = jQuery(this).attr('id');

    var mfpElement = jQuery('#'+theId+' .animate');
    var mfpElementLength = mfpElement.length;

    if(mfpElementLength>1){
        var addto = '<ul>';
        for(var i=0;i<mfpElementLength;i++){
            addto += '<li><span>'+ (i+1) + '</span></li>';
        }
        addto += '</ul>';
        addto += '<div class="clr">&nbsp;</div>';
        jQuery(addto).appendTo('#'+theId+' #mfptabs');
    }

    mfpElement.hide();
    jQuery('#'+theId+' .animate:first').show();
    jQuery('#'+theId+' .animate:first').addClass('activex');
    jQuery('#'+theId+' #mfptabs li:first').addClass('activex');
    mfpElement.css('position','absolute');
    //mfpElement.css('width',mpfAnimateDiv+'%');
    jQuery('#'+theId+' .minifrontpageid').css('height', jQuery('.animate:first').css('height'));

    jQuery('#'+theId+' #mfptabs li').live('click', function(){ 
        var current = jQuery('#'+theId+' #mfptabs li.activex').index();

        var index = jQuery(this).index();
        if( current != index ){
            jQuery('#'+theId+' #mfptabs li.activex').removeClass('activex');
            jQuery(this).addClass('activex'); 


            jQuery('#'+theId+' .minifrontpageid').animate({
                height: jQuery(mfpElement[index]).css('height')
            },
            {
                step: function(now, fx) {
                    jQuery(mfpElement[current]).fadeOut();
                    jQuery(mfpElement[index]).fadeIn();
                }
            });
        }
        return false;
    });
});


function teste(par) {

    var current = (par - 1);
    var prox = (par + 1);

    if (current < 0) {
        current = 2;
    }

    if (prox > 2){
        prox = 0;
    }

    var mfpElement = jQuery('#minifrontpage-99 .animate');
    var mfpElementLength = mfpElement.length;

    var liativo = jQuery('#mfptabs li');

    jQuery('#mfptabs li.activex').removeClass('activex');
    jQuery(liativo[par]).addClass('activex'); 

    jQuery('.minifrontpageid').animate({
        height: jQuery(mfpElement[par]).css('height')
    },
    {
        step: function(now, fx) {
            jQuery(mfpElement[current]).fadeOut();
            jQuery(mfpElement[par]).fadeIn();
        }
    });


    setTimeout(function () {teste(prox)}, 3000);
}

setTimeout(function () {teste(1)}, 3000);

}); });

If you want to scroll the page to the certain point use this (assuming that you have a link with id scrollToThis ) 如果您想将页面滚动到特定位置,请使用此按钮(假设您有一个id为scrollToThis的链接)

    $('html, body').animate({
        scrollTop: $("#scrollToThis").offset().top
    }, 2000);

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

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