当相应的图像滑入时,将CSS类添加到span标记中

Adding a CSS class to span tag when corresponding image is slide in

本文关键字:添加 CSS span 图像      更新时间:2023-09-26

我有一个两阶段的动画,包括一个充满图像的div,右边是一段10个跨度的句子。图像是绝对的,因此它们堆叠在一起,最初有一个负边距,通过溢出来隐藏图像:隐藏。

在阶段1(当页面加载时,在用户悬停在跨度上之前),图像被设置为每个图像5秒的间隔,以无限的方式循环通过图像。当第二个阶段发生时,这个阶段及其间隔将被清除,也就是当您将鼠标悬停在跨度标记上时,相应的图像将滑入其中进行查看。

我已经对第1阶段和第2阶段进行了编码,但我的问题是:在第1阶段,我必须实现它,以便在默认情况下通过图像设置动画时,相应的span标记必须有一个CSS类,就像在第2阶段中悬停在span标记上时一样。

如果有人想摆弄它,下面是代码:

<!--begin:content-->
                <div id="content">
                        <div id="pics">
                            <img src="ADD ANY IMAGE" id="defaultImg" alt="" />
                            <img src="ADD ANY IMAGE" id="hover_1_pic" alt="" />
                            <img src="ADD ANY IMAGE" id="hover_2_pic" alt="" />
                            <img src="ADD ANY IMAGE" id="hover_3_pic" alt="" />
                            <img src="ADD ANY IMAGE" id="hover_4_pic" alt="" />
                            <img src="ADD ANY IMAGE" id="hover_5_pic" alt="" />
                            <img src="ADD ANY IMAGE" id="hover_6_pic" alt="" />
                            <img src="ADD ANY IMAGE" id="hover_7_pic" alt=""  />
                            <img src="ADD ANY IMAGE" id="hover_8_pic" alt="" />
                            <img src="ADD ANY IMAGE" id="hover_9_pic" alt="" />
                            <img src="ADD ANY IMAGE" id="hover_10_pic" alt="" />
                        </div>
                        <!--begin: homeText - block of span tags w/text referenced in jQuery -->
                        <div class="homeText">    
                            <p>
                                <span id="hover_1" >evolve water.</span>
                                <span id="hover_2">stream the party.</span>
                                <br />
                                <span id="hover_3">let moms play.</span>
                                <span id="hover_4">play on big screens.</span>
                                <br /> 
                                <span id="hover_5">turn txt into sport.</span>
                                <span id="hover_6">have 18 wheels.</span>
                                <br />
                                <span id="hover_7">have chapters.</span>
                                <span id="hover_8">personify an issue.</span>
                                <br />
                                <span id="hover_9">transform neighborhoods.</span>
                                <br />
                                <span id="hover_10">become keepsakes</span>
                            </p>
                        </div>
            </div><!--end content-->

CSS

#pics img {
height: 131px;
width: 334px;
position: absolute;
margin-left:-325px;
}
/*  ADDED by ben sewards   */
#pics {
height:179px;
width:335px;
position: relative;
overflow: hidden;
margin:0px;
padding-top:15px;
margin-left:49px;
float:left;
}
/*  ADDED by ben sewards   */
.homeText {
width:600px;
height:240px;
padding-left:15px;
padding-top: 10px;
overflow: hidden;
float:left;
}
.homeText p {
line-height: 115%;
font-family: @Adobe Fangsong Std R;
font-size: 2.6em;
font-weight:bolder;
color: #c0c0c0;
margin: 0px;
}
.homeText span:hover {
background-color:Lime;
color: White;
cursor: pointer;
}
.span-background-change {
background-color:Lime;
color: White;
}

JS脚本

$('document').ready(function () {
slideIn('defaultImg');
timer = setInterval('slideInNext()', 5000);
functionHover();
});
var slideSpeed = 500;
var slideIn = function (id) {
$('#' + id).addClass('active').animate({ 'margin-left': '0px' }, { 'duration':             slideSpeed, 'easing': 'swing', 'queue': true });
}
var slideOutCurrent = function () {
$('#pics img.active').removeClass('active').animate({ 'margin-left': '325px' }, {     'duration': slideSpeed, 'easing': 'swing', 'queue': true, 'complete': function () {         $(this).css('margin-left', '-325px'); } });
}
var slideInNext = function () {
var curImage = $('#pics img.active');
var nextImage = curImage.next();
if (nextImage.length == 0) {
    nextImage = $('#pics img:first');
}
slideOutCurrent();
slideIn(nextImage.attr('id'));
}
var queueToSlideIn = [];
var mouseOnTimer = null;
var mouseOffTimer = null;
var functionHover = function () {
$('.homeText span').hover(
//binding 2 handlers to hover event
function () {   //when hovering over a span - mousenenter
    clearTimeout(mouseOffTimer);
    clearInterval(timer);
    var thisId = $(this).attr('id');
    mouseOnTimer = setTimeout(function () {
        if (!$('#' + thisId + '_pic').hasClass('active')) {
            addToQueue(thisId + '_pic');
        }
    }, 300);
},
function () {   //when off of span - mouseleave
    clearTimeout(mouseOnTimer);
    mouseOffTimer = setTimeout(function () {
        if (!$('#defaultImg').hasClass('active')) {
            addToQueue('defaultImg');
        }
    }, 500);
}
);
$('.homeText span').click(function () {
    //set current span on click
    $span = $(this).attr('id');
    //navigate to corresponding case study
    var href = $('#' + $span + '_pic').attr('alt');
    window.location.href = href;
});
}
var addToQueue = function (id) {
queueToSlideIn.push(id);
$('#pics').queue(function () { animateNext(); $(this).dequeue(); }).delay(slideSpeed);
}
var animateNext = function () {
if (queueToSlideIn.length > 0) {
    var id = queueToSlideIn.shift();
    slideOutCurrent();
    slideIn(id);
}
};

如果压痕很乱,很抱歉。

Ben

我添加了一个新的类,它是悬停类的副本:

    .homeText-hover {
background-color:Lime;
color: White;
cursor: pointer;
}

然后我在SlideIn和slideOutCurrent函数中各添加了两行:

var slideIn = function (id) {
var slId = id.split('_pic');
$('#' + slId[0]).addClass('homeText-hover');
$('#' + id).addClass('active').animate({ 'margin-left': '0px' }, { 'duration':             slideSpeed, 'easing': 'swing', 'queue': true });
}
var slideOutCurrent = function () {
var slId = $('#pics img.active').attr('id').split('_pic');
$('#' + slId[0]).removeClass('homeText-hover');
$('#pics img.active').removeClass('active').animate({ 'margin-left': '325px' }, {     'duration': slideSpeed, 'easing': 'swing', 'queue': true, 'complete': function () {         $(this).css('margin-left', '-325px'); } });
}

你的自动滑动在FF中不工作…

本,我喜欢你的解决方案。另一个使用相同的选择标识属性原则的解决方案是向每个元素添加一个对每个img span对唯一的类,以便每个元素与其对应的元素共享一个特定的类。

以下是对类作为标志的使用的解释,我最初发布在一个解决方案中,该解决方案后来被关闭:


分类为标志

在元素中添加一个类并不总是意味着要给它一些新的CSS样式。CSS是一种使用CLASSES来帮助识别元素以特定方式进行样式设置的语言;类不是将CSS应用于元素的唯一目的。如果不是这样的话,CSS将只能通过使用类来对元素进行样式化,而不能通过使用其他选择器(ID、父对象、子对象、子代等)

开发人员经常使用类作为"标志"。标志是一种在不必将特定元素的信息存储在变量中的情况下发出信号的方式。例如,假设您有一个元素列表,并且所有元素的样式都完全相同,通过一个CSS类。如果开发人员希望在不更改元素样式的情况下,以特定的方式标记该列表中的其他元素(供以后使用),他可以选择在元素中添加第二个名为"alternate"的类。

你可以在一个元素中添加任意多个类,添加多个没有任何关联样式的类是完全可以接受的编码样式(前提是这些类用于其他用途-脚本等)

将这段代码添加到我的幻灯片中下一个函数可获得所需结果:

if (nextImage.attr('id') != "defaultImg") {
    //add class to corresponding span tag of current image
    var spanId = nextImage.attr('id');
    //corresponing span of next image
    spanId = spanId.substring(0, spanId.length - 4);
    $('#' + spanId).addClass('span-background-change');
}

我只是在javascript中使用了substring方法来分离images属性id,并将其放在一个局部变量中以表示span id。