The first step is to draw the outline of the button
Choose the appropriate one html tag, set the outline CSS
/* 接在上面继续写 */ .button:after{ display: block; position: absolute; //相对按钮的轮廓进行决定定位 top:2px; bottom: 2px; //即设置top,又设置bottom使元素自动拉伸到最大 left:2px; //实际上,按钮的宽度即为容器的高度-(top+bottom) width:36px; //按钮的宽度 line-height: 36px; //按钮文字的高度,如果不需要文字,可移除 text-align: center; text-transform: uppercase; font-size: 16px; background-color: #fff; //这里的背景颜色是按钮的背景颜色 border:2px solid; transition: all 500ms; //按钮的动画时长 }
.off:after { content: 'off'; border-radius:30px; color: #999; }
- 再接着绘制要切换的状态
.on:after { content: 'ON'; border-radius:30px; transform: translate(56px, 0); color:transparent; background-color:#4BD429; }
##imitate IOS-3. jpg
In fact, among CSS switches, toggleClass is the most convenient.
but! ! !This switching method cannot switch the JS event you want to trigger.
After all, we draw buttons to complete certain functions,
So I adopted this method, but it may not be the best
var zn=0; $('.button').click(function(e){ if(zn==1){ $(this).removeClass("on").addClass("off"); //此处可填要触发的事件 zn=0; }else{ $(this).removeClass("off").addClass("on"); //此处可填要触发的事件 zn=1; } });
绘制过程并不复杂,我也就不细说了,贴下效果图和代码,感兴趣的可以自行看一下
##Simulation switch.jpg
Simulation-2.jpg
PS:我引用了一个初始化的CSS文件,可能需要 box-sizing:border-box; /* JS代码 */
The above is the detailed content of Sample code sharing on how to use CSS3+JS to draw various buttons. For more information, please follow other related articles on the PHP Chinese website!