作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有一个很酷的开关盒的 html 和 css,我将把它用作我网站上的语言开关。但是,由于我是 JavaScript 的初学者,我缺少执行重定向操作的处理程序。
这是我到目前为止的工作。问题出在 JavaScript 中。
$(".flag-switch input").click(function(){$(this).is(":checked")?setTimeout(function(){window.location.href="en/"},250):setTimeout(function(){window.location.href="../"},250)}
.flag-switch {
position: relative;
right: -30px;
top: 4px;
outline: 0;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
-webkit-perspective: 1000;
-webkit-backface-visibility: hidden;
-webkit-transform: translate3d(0, 0, 0);
-webkit-tap-highlight-color: transparent;
width: 60px;
height: 30px;
margin: 5px auto;
}
.flag-switch:before, .flag-switch:after {
position: absolute;
color: white;
font-family: 'Raleway', sans-serif;
top: 8px;
font-size: 13px;
font-weight: 700;
}
.flag-switch:before {
left: -19px;
content: attr(data-first-lang);
}
.flag-switch:after {
right: -17px;
content: attr(data-second-lang);
}
.flag-switch input {
display: none;
}
.flag-switch input + label {
display: block;
position: absolute;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
width: 60px;
}
.flag-switch input + label:before, .flag-switch input + label:after {
content: "";
position: absolute;
border-radius: 30px;
-webkit-transition: all 0.25s ease-in-out;
transition: all 0.25s ease-in-out;
}
.flag-switch input + label:before {
height: 30px;
width: 60px;
background-color:#EE4B53 ;
}
.flag-switch input + label:after {
top: 3px;
left: 3px;
border: 2px solid #DFDFDF;
width: 24px;
height: 24px;
background: #eb3 url(http://i.imgur.com/mYEJa6E.png) -26px center;
transform: translate(30px, 0);
-webkit-transform: translate(30px, 0);
}
.flag-switch input:checked + label:after {
background: #000233 url(http://i.imgur.com/mYEJa6E.png) -2px center;
-webkit-transition: all 0.25s ease-in-out;
transition: all 0.25s ease-in-out;
-webkit-transform: translate(0, 0);
transform: translate(0, 0);
}
.flag-switch input:checked + label:before {
background-color: #1FA9D6;
}
<div class="flag-switch" data-first-lang="GR" data-second-lang="EN">
<input type="checkbox" id="check1" checked>
<label for="check1"></label>
</div>
最佳答案
相同的 Javascript 版本:
document.querySelector('#check1').addEventListener('change', function(){
if(this.checked){
setTimeout(function(){window.location.href="en/"},250);
}
else{
setTimeout(function(){window.location.href="../"},250);
}
});
.flag-switch {
position: relative;
right: -30px;
top: 4px;
outline: 0;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
-webkit-perspective: 1000;
-webkit-backface-visibility: hidden;
-webkit-transform: translate3d(0, 0, 0);
-webkit-tap-highlight-color: transparent;
width: 60px;
height: 30px;
margin: 5px auto;
}
.flag-switch:before, .flag-switch:after {
position: absolute;
color: white;
font-family: 'Raleway', sans-serif;
top: 8px;
font-size: 13px;
font-weight: 700;
}
.flag-switch:before {
left: -19px;
content: attr(data-first-lang);
}
.flag-switch:after {
right: -17px;
content: attr(data-second-lang);
}
.flag-switch input {
display: none;
}
.flag-switch input + label {
display: block;
position: absolute;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
width: 60px;
}
.flag-switch input + label:before, .flag-switch input + label:after {
content: "";
position: absolute;
border-radius: 30px;
-webkit-transition: all 0.25s ease-in-out;
transition: all 0.25s ease-in-out;
}
.flag-switch input + label:before {
height: 30px;
width: 60px;
background-color:#EE4B53 ;
}
.flag-switch input + label:after {
top: 3px;
left: 3px;
border: 2px solid #DFDFDF;
width: 24px;
height: 24px;
background: #eb3 url(http://i.imgur.com/mYEJa6E.png) -26px center;
transform: translate(30px, 0);
-webkit-transform: translate(30px, 0);
}
.flag-switch input:checked + label:after {
background: #000233 url(http://i.imgur.com/mYEJa6E.png) -2px center;
-webkit-transition: all 0.25s ease-in-out;
transition: all 0.25s ease-in-out;
-webkit-transform: translate(0, 0);
transform: translate(0, 0);
}
.flag-switch input:checked + label:before {
background-color: #1FA9D6;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div class="flag-switch" data-first-lang="GR" data-second-lang="EN">
<input type="checkbox" id="check1" >
<label for="check1"></label>
</div>
关于javascript - 使用 jQuery 重定向复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35635635/
我是一名优秀的程序员,十分优秀!