简体   繁体   English

滚动到jcarousel的第一项

[英]Scroll to first item of jcarousel

I have multiple instances of jcarousel on a page within a tabbed interface. 我在选项卡式界面的页面上有多个jcarousel实例。 I need to be able to scroll to the first item of each carousel when the relevant tab is clicked and am unsure how to do this. 我需要能够在单击相关选项卡时滚动到每个轮播的第一项,并且我不确定如何执行此操作。 I've looked at the static controls example ( http://sorgalla.com/projects/jcarousel/examples/static_controls.html ) but cannot fathom how to get this working for multiple carousels. 我已经看过静态控件示例( http://sorgalla.com/projects/jcarousel/examples/static_controls.html ),但无法理解如何使其适用于多个轮播。

Any help would be greatly appreciated. 任何帮助将不胜感激。 My work-in-progress is here: http://www.brainsdesign.com/client/Lab/14512/style.html 我的工作进展如下: http//www.brainsdesign.com/client/Lab/14512/style.html

Many thanks, 非常感谢,

Chris 克里斯

You can use something like: 您可以使用以下内容:

jQuery('#myCarousel')
     .jcarousel('scroll',position); 

Where position is the start of your jcarousel or the index you want to get to. 位置是您的jcarousel的起点或您想要的索引。

This is from the jquery.jcarousel.js source file: 这是来自jquery.jcarousel.js源文件:

 /**
     * Scrolls the carousel to a certain position.
     *
     * @method scroll
     * @return undefined
     * @param i {Number} The index of the element to scoll to.
     * @param a {Boolean} Flag indicating whether to perform animation.
     */
    scroll: function(i, a) {
        if (this.locked || this.animating) {
            return;
        }

        this.pauseAuto();
        this.animate(this.pos(i), a);
    },

To scroll to a specific arbitrary position in jCarousel... 滚动到jCarousel中的特定任意位置...

  1. Get the jcarousel instance object. 获取jcarousel实例对象。 It's in the jQuery .data() of the element that .jcarousel() was called on (side note: in Drupal views jcarousel module, that's the ul.jcarousel ) 它是在调用.jcarousel()的元素的jQuery .data()中(旁注:在Drupal视图中jcarousel模块,这是ul.jcarousel
  2. Call .scroll() on it. 在上面调用.scroll()

In code: 在代码中:

// Create it
$('.posts').jcarousel( someSettings );

// Get it
var jcarousel = $('.posts').data( 'jcarousel' );

// Scroll it
var scrollTo = 1;
var animateScrolling = true;

jcarousel.scroll( scrollTo - 1, animateScrolling );

If ever want to look up a specific element using jQuery selectors, then, scroll to that element (scrolling a jCarousel by element not by position). 如果想要使用jQuery选择器查找特定元素,则滚动到该元素(按元素而不是按位置滚动jCarousel)。 It's easy: each jCarousel element has an attribute, jcarouselindex. 这很简单:每个jCarousel元素都有一个属性jcarouselindex。 Look it up with var position = $('#some-element').attr('jcarouselindex'); 查找var position = $('#some-element').attr('jcarouselindex'); .

Sample: 样品:

// Get jcarousel
var jcarousel = $('#menu').data('jcarousel');

var scrollTo = menuOption.parent().attr("jcarouselindex");
var animateScrolling = true;

// Scroll it
jcarousel.scroll(scrollTo - 1, animateScrolling);

where menuOption is an anchor <a> like this one: 其中menuOption是一个像这样的锚<a>

<li class="jcarousel-item jcarousel-item-horizontal jcarousel-item-8 jcarousel-item-8-horizontal" style="float: left; list-style: none outside none;" jcarouselindex="8">
<a title="Educação de Pacientes e Familiares" data-chapterid="16" data-acronym="PFE" href="">
</li>

Note: it's important to use scrollTo - 1 because index is 0 based . 注意:使用scrollTo - 1很重要,因为index是基于0的

Here is the solution, managed it to work form 这是解决方案,将其管理为工作表单

http://sorgalla.com/projects/jcarousel/examples/static_controls.html http://sorgalla.com/projects/jcarousel/examples/static_controls.html

I have a tabbed interface like: 我有一个标签界面,如:

<div class="navTabs">
<ul class="tabs">
<li><a></a></li>
<li><a></a></li>
<li><a></a></li>
</ul>
</div>

And each tab contains jcarousel slider. 每个标签包含jcarousel滑块。

jQuery(document).ready(
function($)
{

    jQuery('.posts').jcarousel({
        scroll: 4,
        visible:4,
        //this.scroll(1, 0);
        initCallback: mycarousel_initCallback
    });
});

function mycarousel_initCallback(carousel) {
    jQuery('.navTabs a').bind('click', function() {
        carousel.scroll(1,0);
        //return false;
    });
};

I have checked your site...so thinking you should be able to get the code from here. 我检查了你的网站......所以我认为你应该能够从这里获得代码。

In the jCarousel call a function by initCallBack and trigger the custom function when the tab is clicked do go general scroll to position 1 to reset! 在jCarousel中通过initCallBack调用一个函数并在单击选项卡时触发自定义函数do do general scroll to position 1 to reset!

Thats it. 而已。

Thanks, Enayet 谢谢,Enayet

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

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