如何让一系列元素一次只显示一个

时间:2019-04-26 05:05:22

标签: javascript jquery css loops

我正在尝试使跨度元素agencyText一次出现一个。然后,在要应用的currnext类的间隔结束时正确定位要显示的当前文本和下一个文本。

截至目前,我一次都显示了它们。我用数据属性索引值创建了它们。但是,我不确定如何遍历它们以使其正常工作。

If you click this link左侧是我尝试执行的操作的示例。它是文本旋转90度。每四秒钟更新一次措词。

有人知道我如何修改我的代码以复制它吗?

更新-显示示例的DOM代码。

enter image description here

var arr = $('.textContainer');
var arrLen = arr.length;
var i = 0;
var attr = $('agencyText').data('index');

var loop = function() {		
  var item = arr.eq(i);
  var description = item.find('.agencyText');

  description.addClass('curr');

  i = (i + 1) % arrLen;
};
loop();
setInterval(loop, 3000);
.digitalAgency {
    height: 20px;
    position: fixed;
    top: 50%;
    left: -45%;
    width: 100%;
    text-transform: uppercase;
    font-weight: 700;
    font-size: 14px;
    letter-spacing: 1px;
	-webkit-transition: opacity .5s;
    -o-transition: opacity .5s;
    transition: opacity .5s;
	-webkit-transition:opacity .5s;
	-o-transition:opacity .5s;
	transition:opacity .5s;
}
.digitalAgency .textContainer::before {
	content: "";
	position: absolute;
	width: 40px;
	height: 1px;
	background: #000;
	display: inline-block;
	top: 50%;
	margin-right: 20px;
	left: 33%;
}
.digitalAgency .textContainer {
    position: relative;
    width: 650px;
    height: 100%;
    left: 50%;
    -webkit-transform: translateX(-50%) rotate(-90deg);
    -ms-transform: translateX(-50%) rotate(-90deg);
    transform: translateX(-50%) rotate(-90deg);
    display: inline-block;
}
.digitalAgency, .agencyText {
    -webkit-animation-duration: .45s;
    animation-duration: .45s;
    -webkit-animation-fill-mode: forwards;
    animation-fill-mode: forwards;
    -webkit-animation-timing-function: ease-in-out;
    animation-timing-function: ease-in-out;
}
.digitalAgency .textContainer .agencyText {
    padding-left: 60px;
    position: absolute;
    visibility: hidden;
    width: 100%;
    height: 100%;
    left: 33%;
}
.digitalAgency .textContainer .agencyText.curr {
	visibility: visible;
}
.digitalAgency .agencyText.curr{visibility:visible;-webkit-animation-name:dgAgnCurr;animation-name:dgAgnCurr}
.digitalAgency .agencyText.next{visibility:visible;-webkit-animation-name:dgAgnNext;animation-name:dgAgnNext}
@-webkit-keyframes dgAgnCurr{from{-webkit-transform:translateY(0);transform:translateY(0)}to{-webkit-transform:translateY(-100%);transform:translateY(-100%)}}
@keyframes dgAgnCurr{from{-webkit-transform:translateY(0);transform:translateY(0)}to{-webkit-transform:translateY(-100%);transform:translateY(-100%)}}
@-webkit-keyframes dgAgnNext{from{-webkit-transform:translateY(100%);transform:translateY(100%)}to{-webkit-transform:translateY(0);transform:translateY(0)}}
@keyframes dgAgnNext{from{-webkit-transform:translateY(100%);transform:translateY(100%)}to{-webkit-transform:translateY(0);transform:translateY(0)}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="digitalAgency">
  <span class="textContainer">
    <span data-index="0" class="agencyText">The first one</span>
    <span data-index="1" class="agencyText">The second one</span>
    <span data-index="2" class="agencyText">The third one</span>
    <span data-index="3" class="agencyText">The fourth one</span>
  </span>
</div>

1 个答案:

答案 0 :(得分:1)

我已经更新了您的JavaScript代码。尝试这样

var arr = $('.textContainer > span');
var arrLen = arr.length;
var i = 0;

var loop = function() {		
  $('.textContainer > span').removeClass('curr');
  $('span[data-index = '+i+']').addClass('curr');
  
  i = (i + 1) % arrLen;
  
};
loop();
setInterval(loop, 3000);
.digitalAgency {
    height: 20px;
    position: fixed;
    top: 50%;
    left: -45%;
    width: 100%;
    text-transform: uppercase;
    font-weight: 700;
    font-size: 14px;
    letter-spacing: 1px;
	-webkit-transition: opacity .5s;
    -o-transition: opacity .5s;
    transition: opacity .5s;
	-webkit-transition:opacity .5s;
	-o-transition:opacity .5s;
	transition:opacity .5s;
}
.digitalAgency .textContainer::before {
	content: "";
	position: absolute;
	width: 40px;
	height: 1px;
	background: #000;
	display: inline-block;
	top: 50%;
	margin-right: 20px;
	left: 33%;
}
.digitalAgency .textContainer {
    position: relative;
    width: 650px;
    height: 100%;
    left: 50%;
    -webkit-transform: translateX(-50%) rotate(-90deg);
    -ms-transform: translateX(-50%) rotate(-90deg);
    transform: translateX(-50%) rotate(-90deg);
    display: inline-block;
}
.digitalAgency, .agencyText {
    -webkit-animation-duration: .45s;
    animation-duration: .45s;
    -webkit-animation-fill-mode: forwards;
    animation-fill-mode: forwards;
    -webkit-animation-timing-function: ease-in-out;
    animation-timing-function: ease-in-out;
}
.digitalAgency .textContainer .agencyText {
    padding-left: 60px;
    position: absolute;
    visibility: hidden;
    width: 100%;
    height: 100%;
    left: 33%;
}
.digitalAgency .textContainer .agencyText.curr {
	visibility: visible;
}
.digitalAgency .agencyText.curr{visibility:visible;-webkit-animation-name:dgAgnCurr;animation-name:dgAgnCurr}
.digitalAgency .agencyText.next{visibility:visible;-webkit-animation-name:dgAgnNext;animation-name:dgAgnNext}
@-webkit-keyframes dgAgnCurr{from{-webkit-transform:translateY(0);transform:translateY(0)}to{-webkit-transform:translateY(-100%);transform:translateY(-100%)}}
@keyframes dgAgnCurr{from{-webkit-transform:translateY(0);transform:translateY(0)}to{-webkit-transform:translateY(-100%);transform:translateY(-100%)}}
@-webkit-keyframes dgAgnNext{from{-webkit-transform:translateY(100%);transform:translateY(100%)}to{-webkit-transform:translateY(0);transform:translateY(0)}}
@keyframes dgAgnNext{from{-webkit-transform:translateY(100%);transform:translateY(100%)}to{-webkit-transform:translateY(0);transform:translateY(0)}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="digitalAgency">
  <span class="textContainer">
    <span data-index="0" class="agencyText">The first one</span>
    <span data-index="1" class="agencyText">The second one</span>
    <span data-index="2" class="agencyText">The third one</span>
    <span data-index="3" class="agencyText">The fourth one</span>
  </span>
</div>