gpt4 book ai didi

javascript - 制作背景幻灯片动画

转载 作者:太空狗 更新时间:2023-10-29 16:34:23 24 4
gpt4 key购买 nike

我制作了一个按钮组,当用户选择上一个或下一个按钮的每个按钮背景移动/滑动到选定的一个时,我用纯 css 制作了这个效果并且只使用了 jquery 添加或删除 active 类。现在的问题是当你点击 All 按钮,然后点击 New 它工作正常,但是如果你点击 Used 幻灯片就不能正常工作位置。

为了这个效果我使用了transition::before,需要用纯css或者最小的jquery来解决这个问题,不是很多javascriptjquery 代码。

移动这个背景的逻辑是:

.RadioButton .btn:first-child::before {
right: 0;
transition: .3s all ease;
}

.RadioButton .btn:nth-child(2)::before {
transition: .3s all ease;
}

.RadioButton .btn:last-child::before {
left: 0;
transition: .3s all ease;
}

关于 .RadioButton .btn:nth-child(2)::before 的问题我不能使用 right:0left:0 因为如果我使用每一个,幻灯片效果在正确的位置不起作用,有什么解决办法吗?

到目前为止我尝试了什么:

$('.RadioButton').each(function(){
$(this).find('.btn').each(function(){
$(this).click(function(){
$(this).parent().find('.btn').removeClass('btn-active');
$(this).addClass('btn-active');
});
});
});
body {
direction: rtl;
}

.RadioButton .btn {
width: 33%;
float: left;
margin: 0;
box-sizing: border-box;
position: relative;
font-size: 11px;
min-height: 30px!important;
line-height: 30px;
border-radius: 5px;
overflow: hidden;
cursor: pointer;
text-align: center;
}

.btn-default {
border: 1px solid gray;
color: gray;
}

.btn-group>.btn:first-child {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}

.btn-group>.btn:nth-child(2) {
border-radius: 0;
}

.btn-group>.btn:last-child {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}

.RadioButton .btn::before {
content: "";
height: 100%;
width: 0%;
background: gray;
position: absolute;
top: 0;
transition: .3s all ease;
z-index: -1;
}

.btn-active::before {
width: 100%!important;
}

.RadioButton .btn:nth-child(2)::before {
right: 0;
transition: .3s all ease;
}

.RadioButton .btn:last-child::before {
left: 0;
transition: .3s all ease;
}

.btn-active:first-child::before {
left: 0;
transition: .3s all ease;
}

.btn-active:last-child::before {
left: 0;
transition: .3s all ease;
}

.btn.btn-default.btn-active {
color: white;
}

.btn-group {
clear: both;
margin-top: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="RadioButton btn-group" id="searchType">
<a class="btn btn-default" data-val="0">Used</a>
<a class="btn btn-default" data-val="1">New</a>
<a class="btn btn-default btn-active" data-val="2">All</a>
</div>

最佳答案

在最后一个按钮上仅在伪之后设置一个。现在,使用同级选择器和事件类将其适本地向左移动。 (查看 CSS 中的最后两个样式)

$('.RadioButton').each(function(){
$(this).find('.btn').each(function(){
$(this).click(function(){
$(this).parent().find('.btn').removeClass('btn-active');
$(this).addClass('btn-active');
});
});
});
body {
direction: rtl;
}

.RadioButton .btn {
width: 33%;
float: left;
margin: 0;
box-sizing: border-box;
position: relative;
font-size: 11px;
min-height: 30px!important;
line-height: 30px;
border-radius: 5px;
cursor: pointer;
text-align: center;
}

.btn-default {
border: 1px solid gray;
color: gray;
}

.btn-group>.btn:first-child {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}

.btn-group>.btn:nth-child(2) {
border-radius: 0;
}

.btn-group>.btn:last-child {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
.btn.btn-default.btn-active {
color: white;
}

.btn-group {
clear: both;
margin-top: 10px;
}

.RadioButton .btn:nth-child(3)::before {
content: "";
height: 100%;
width: 100%;
background: gray;
position: absolute;
top: 0;
transition: .3s all ease;
z-index: -1;
left: 0px;
}

.btn-active:first-child ~ .btn::before {
transform: translateX(calc(-4px - 200%));
}
.btn-active:nth-child(2) ~ .btn::before {
transform: translateX(calc(-2px - 100%));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="RadioButton btn-group" id="searchType">
<a class="btn btn-default" data-val="0">Used</a>
<a class="btn btn-default" data-val="1">New</a>
<a class="btn btn-default btn-active" data-val="2">All</a>
</div>

关于javascript - 制作背景幻灯片动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47675041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com