2

Jquery serialScrollプラグインを使用していて、現在の画像に.activeクラスを追加する水平ギャラリーを作成しました。また、現在の画像のタイトルを取得して、ページの段落タグ内に表示したいと思います。

私のHTML:

<p class="title"></p>

<div id="slideshow">

<ul>
<li><img src="www.website.com/1.jpg title="title 1" /></li>
<li><img src="www.website.com/2.jpg title="title 2" /></li>
<li><img src="www.website.com/3.jpg title="title 3" /></li>
<li><img src="www.website.com/4.jpg title="title 4" /></li>
</ul>

</div>

私のJavacript:

// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/


 jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
    return -c * ((t = t / d - 1) * t * t * t - 1) + b;
};

jQuery(function ($) {

    var $nav = $('#slideshow li');


    $('#slideshow').serialScroll({
        items: 'li',
        prev: '.prev',
        next: '.next',
        offset: 0, //when scrolling to photo, stop 230 before reaching it (from the left)
        start: 0, //as we are centering it, start at the 2nd
        duration: 1000,
        force: false,
        stop: true,
        constant: false,
        lock: false,
        cycle: false, //don't pull back once you reach the end
        easing: 'easeOutQuart', //use this easing equation for a funny effect
        jump: true, //click on the images to scroll to them
        navigation: $nav,
        onBefore: function (e, el, $p, $i, pos) {
            $nav.removeClass('newclass');
            $nav.eq(pos).addClass('newclass')
        },

    });
});

タイトルを取得するには、どういうわけかこのJavascriptを追加する必要があると思います。

<script>
var title = $("li.newclass img").attr("title");
$("p.title").text(title);
</script>
4

1 に答える 1

0

最初にhtmlを修正し、終了引用符を追加して、次のようにhref部分を修正します。

<p class="title"></p>

<div id="slideshow">

<ul>
<li><img src="http://www.website.com/1.jpg" title="title 1" /></li>
<li><img src="http://www.website.com/2.jpg" title="title 2" /></li>
<li><img src="http://www.website.com/3.jpg" title="title 3" /></li>
<li><img src="http://www.website.com/4.jpg" title="title 4" /></li>
</ul>

</div>

Pのテキストを画像のタイトルと同じにするには、クラスを変更する場所と同じ関数を使用します...次のようになります。

// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
    return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

jQuery(function( $ ){

    var $nav = $('#slideshow li');
    var pos = 0;
    $nav.removeClass('newclass');
    $nav.eq(pos).addClass('newclass')
    var title = $("li.newclass img").attr("title");
    $("p.title").text(title);

    $('#slideshow').serialScroll({
        items:'li',
        prev:'.prev',
        next:'.next',
        offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
        start:0, //as we are centering it, start at the 2nd
        duration:1000,
        force:false,
        stop:true,
        constant:false,
        lock:false,
        cycle:false, //don't pull back once you reach the end
        easing:'easeOutQuart', //use this easing equation for a funny effect
        jump: true, //click on the images to scroll to them
        navigation:$nav,
        onBefore:function(e,el,$p,$i,pos){
            $nav.removeClass('newclass');
            $nav.eq(pos).addClass('newclass')
            var title = $("li.newclass img").attr("title");
            $("p.title").text(title);
        }
    });
});
于 2012-09-17T06:45:41.077 に答える