Seb*_*a99 9 html css css-transitions css-shapes
当我点击我的按钮时,我正在尝试做一个小动画.
我希望背景向左和向右滑动以说明用户的选择.
这是我的试用版:JSFiddle
$(".test").click(function() {
$(".test").removeClass('active');
$(this).addClass('active');
})Run Code Online (Sandbox Code Playgroud)
div{
list-style-type: none;
}
div a {
text-decoration: none;
}
#cont {
background-color: white;
width: 100%;
padding: 2px;
height: 40px;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
}
.choice {
float: left;
margin-right: 1px;
}
.choice div{
width: 100px;
padding: 11px 16px;
text-align: center;
float: left;
background: #ff3232;
margin-left: 10px;
transition: all 0.4s linear;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
}
.choice .left {
background: linear-gradient(to right, white 50%, #b7d4e1 50%);
background-size: 200% 100%;
background-position: left bottom;
}
.choice .right {
background: linear-gradient(to right, #b7d4e1 50%, white 50%);
background-size: 200% 100%;
background-position: right bottom;
}
.choice .left.active {
background-position: right bottom;
}
.choice .right.active {
background-position: left bottom;
}
.choice div a {
color: black;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="choice">
<div id="cont">
<div class="test left active"><a href="#">Semaine</a>
</div>
<div class="test right"><a href="#">Mois</a>
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
但是我希望背景能够长时间保持所有幻灯片的形状.
根据您的使用情况,您可以使用药片突出显示没有JS 的点击元素.
重点是使用CSS过渡并播放转换延迟,以便药丸保持在最后一个聚焦链接之下.
药丸是额外的跨度和border-radius.它是在重点链接下翻译的:
.choice{
background-color: white;
float: left;
padding: 2px;
height: 40px;
border-radius: 20px;
position:relative;
z-index:1;
}
.link {
width: 100px;
padding: 11px 16px;
text-align: center;
float: left;
text-decoration:none;
color:#000;
}
.pill{
position:absolute;
left:16px; top:0;
width:100px; height:100%;
background:#B7D4E1;
border-radius:20px;
z-index:-1;
transition: 9999s transform .2s ease-out;
}
.link:nth-child(2):focus ~ .pill{
transform: translatex(132px);
transition: transform .18s ease-out;
}
.link:nth-child(1):focus ~ .pill{
transform: translatex(0px);
transition: transform .18s ease-out;
}Run Code Online (Sandbox Code Playgroud)
<div class="choice">
<a class="link" href="#">Semaine</a>
<a class="link" href="#">Mois</a>
<span class="pill"></span>
</div>Run Code Online (Sandbox Code Playgroud)