如何在段落标签内的 JQuery 序列化滚动插件库中添加图像的标题



我正在使用Jquery serialScroll插件,并制作了一个水平图库,该图库将.active类添加到当前图像中。我还想获取当前图像的标题并将其显示在段落标签内的页面上。

我的网页:

<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>

我的爪哇脚本:

// 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>

首先更正您的 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);
        }
    });
});

最新更新