应无所住,而生其心
排名
1
文章
860
粉丝
112
评论
163
net core webapi post传递参数
庸人 : 确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

css实现开关组件效果,滑块组件效果。自己实现小程序滑块开关组件

1475人阅读 2024/6/5 11:49 总访问:5182726 评论:0 收藏:0 手机
分类: 前端

效果如下


切换的时候会有动态的效果

代码如下,纯HTML+CSS

样式里边的注释都写得比较清晰了,需要自定义效果,圆角什么的自行修改即可

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .switch {
  9. position: relative;
  10. display: inline-block;
  11. width: 108px;
  12. height: 34px;
  13. }
  14. .switch input {
  15. opacity: 0;
  16. width: 0;
  17. height: 0;
  18. }
  19. .slider {
  20. position: absolute;
  21. cursor: pointer;
  22. top: 0;
  23. left: 0;
  24. right: 0;
  25. bottom: 0;
  26. /* background: #F1F4F5; */
  27. background: #eee;
  28. border-radius: 8rpx 8rpx 8rpx 8rpx;
  29. -webkit-transition: .4s;
  30. transition: .4s;
  31. }
  32. .slider:before {
  33. position: absolute;
  34. content: "";
  35. /* content: "未读"; */
  36. height: 26px;
  37. width: 46px;
  38. left: 4px;
  39. text-align: center;
  40. line-height: 26px;
  41. /* color: #5EC6A7; */
  42. bottom: 4px;
  43. background-color: white;
  44. border-radius: 8rpx 8rpx 8rpx 8rpx;
  45. -webkit-transition: .4s;
  46. transition: .4s;
  47. }
  48. /* 如果选中的状态下有背景颜色,边框,阴影这些变化可以使用下面的样式 */
  49. /* input:checked+.slider {
  50. background-color: #2196F3;
  51. }
  52. input:focus+.slider {
  53. box-shadow: 0 0 1px #2196F3;
  54. } */
  55. /*
  56. 动画切换效果,就是那个伪类块块的滑动动画
  57. input:checked 的意思是当input标签选中的时候,具体用的时候可以多加一点限制,
  58. 或者直接用一个具体的样式限制,比如可以修改为.switch_input:checked+.slider:before
  59. +.slider 的意思就是选中input标签的相邻的兄弟节点
  60. .slider:before 这里就是样式slider后面的伪类选择器
  61. */
  62. input:checked+.slider:before {
  63. -webkit-transform: translateX(53px);
  64. -ms-transform: translateX(53px);
  65. transform: translateX(53px);
  66. }
  67. /*
  68. 选中状态下 左边的文字位置与样式
  69. 这里样式的意思就是input选中的时候找到它相邻的兄弟节点slider,然后找到slider的后代元素lefTag
  70. */
  71. input:checked+.slider .lefTag {
  72. color: #313960;
  73. }
  74. /* 选中状态下 右边的文字位置与样式 */
  75. input:checked+.slider .rightTag {
  76. color: #5EC6A7;
  77. }
  78. /* 默认状态下左边的文字位置与样式 */
  79. .lefTag {
  80. position: absolute;
  81. left: 12px;
  82. top: 7px;
  83. font-size: 15px;
  84. color: #5EC6A7;
  85. }
  86. /* 默认状态下右边的文字位置与样式 */
  87. .rightTag {
  88. position: absolute;
  89. right: 13px;
  90. top: 7px;
  91. font-size: 15px;
  92. color: #313960;
  93. }
  94. </style>
  95. </head>
  96. <body>
  97. <label class="switch">
  98. <input type="checkbox" class="switch_input" hidden>
  99. <div class="slider round">
  100. <div class="lefTag">未读</div>
  101. <div class="rightTag">全部</div>
  102. </div>
  103. </label>
  104. </body>
  105. </html>

用于uni-app开发编译到微信小程序开发工具运行的

用于uni-app开发使用npm run dev:mp-weixin编译到微信小程序开发工具运行调试的,开发工具是用的是vscode

效果图如下:

如果直接把上面的代码和样式用于小程序中是运行不起的,input type="checkbox"这个在微信小程序中没有效果的,所以改变了一下实现思路,直接都用view来实现,然后是否选中的状态用点击事件和vue的数据绑定来实现,还有一点就是那个移动滑块的实现从伪类换成了view。

代码如下,样式像素单位是px版本

具体关键代码的实现注释即可

  1. <template>
  2. <view class="message-container">
  3. <view class="titleWrap">
  4. <view class="title">{{ state.title }}</view>
  5. <view>
  6. <view class="switch" @tap="swichChoise">
  7. <view class="switch_input" hidden />
  8. <!-- 这里当属性state.isChoise为true的时候就会多一个选中状态下的样式sliderRun -->
  9. <view class="slider round" :class="{ sliderRun: state.isChoise }">
  10. <view class="lefTag">未读</view>
  11. <view class="rightTag">全部</view>
  12. <view class="swtichTag"></view>
  13. </view>
  14. </view>
  15. </view>
  16. </view>
  17. <view class="splitline"></view>
  18. </view>
  19. </template>
  20. <script setup lang="ts" name="tasks">
  21. import { defineAsyncComponent, reactive, onMounted, toRefs, ref } from 'vue';
  22. const state = reactive({
  23. title: '消息通知',
  24. isChoise: false,
  25. })
  26. onMounted(() => {
  27. })
  28. // 切换选中状态
  29. const swichChoise = () => {
  30. state.isChoise = !state.isChoise
  31. }
  32. </script>
  33. <style scoped="scoped" lang="scss">
  34. .message-container {
  35. padding: 15px;
  36. .titleWrap {
  37. display: flex;
  38. justify-content: space-between;
  39. align-items: center;
  40. .title {
  41. font-family: PingFang SC, PingFang SC;
  42. font-weight: 400;
  43. font-size: 30rpx;
  44. color: #313960;
  45. }
  46. }
  47. .splitline {
  48. margin-left: -20rpx;
  49. margin-right: -20rpx;
  50. margin-top: 30rpx;
  51. height: 1rpx;
  52. background: #f3f3f3;
  53. }
  54. }
  55. </style>
  56. <!-- 滑块开关按钮的样式 -->
  57. <style scoped="scoped" lang="scss">
  58. .switch {
  59. position: relative;
  60. display: inline-block;
  61. width: 108px;
  62. height: 34px;
  63. }
  64. .switch input {
  65. opacity: 0;
  66. width: 0;
  67. height: 0;
  68. }
  69. .slider {
  70. position: absolute;
  71. top: 0;
  72. left: 0;
  73. right: 0;
  74. bottom: 0;
  75. background: #F1F4F5;
  76. border-radius: 8rpx 8rpx 8rpx 8rpx;
  77. -webkit-transition: .4s;
  78. transition: .4s;
  79. }
  80. .swtichTag {
  81. position: absolute;
  82. height: 26px;
  83. width: 46px;
  84. left: 4px;
  85. text-align: center;
  86. line-height: 26px;
  87. bottom: 4px;
  88. background-color: white;
  89. border-radius: 8rpx 8rpx 8rpx 8rpx;
  90. -webkit-transition: .4s;
  91. transition: .4s;
  92. }
  93. .sliderRun {
  94. // 选中状态下滑块往右边移动
  95. .swtichTag {
  96. -webkit-transform: translateX(53px);
  97. -ms-transform: translateX(53px);
  98. transform: translateX(53px);
  99. }
  100. /* 选中状态下 左边的文字位置与样式 */
  101. .lefTag {
  102. color: #313960;
  103. }
  104. /* 选中状态下 右边的文字位置与样式 */
  105. .rightTag {
  106. color: #5EC6A7;
  107. }
  108. }
  109. /* 默认状态下左边的文字位置与样式 */
  110. .lefTag {
  111. position: absolute;
  112. left: 12px;
  113. top: 7px;
  114. font-size: 15px;
  115. color: #5EC6A7;
  116. z-index: 999;
  117. }
  118. /* 默认状态下右边的文字位置与样式 */
  119. .rightTag {
  120. position: absolute;
  121. right: 13px;
  122. top: 7px;
  123. font-size: 15px;
  124. color: #313960;
  125. z-index: 999;
  126. }
  127. </style>

样式像素单位是rpx版本

如果是运行到微信小程序的,最好像素单位还是使用rpx

  1. <template>
  2. <view class="message-container">
  3. <view class="titleWrap">
  4. <view class="title">{{ state.title }}</view>
  5. <view>
  6. <view class="switch" @tap="swichChoise">
  7. <view class="switch_input" hidden />
  8. <!-- 这里当属性state.isChoise为true的时候就会多一个选中状态下的样式sliderRun -->
  9. <view class="slider round" :class="{ sliderRun: state.isChoise }">
  10. <view class="lefTag">未读</view>
  11. <view class="rightTag">全部</view>
  12. <view class="swtichTag"></view>
  13. </view>
  14. </view>
  15. </view>
  16. </view>
  17. <view class="splitline"></view>
  18. </view>
  19. </template>
  20. <script setup lang="ts" name="tasks">
  21. import { defineAsyncComponent, reactive, onMounted, toRefs, ref } from 'vue';
  22. const state = reactive({
  23. title: '消息通知',
  24. isChoise: false,
  25. })
  26. onMounted(() => {
  27. })
  28. // 切换选中状态
  29. const swichChoise = () => {
  30. state.isChoise = !state.isChoise
  31. }
  32. </script>
  33. <style scoped="scoped" lang="scss">
  34. .message-container {
  35. padding: 30rpx 20rpx 10rpx 20rpx;
  36. .titleWrap {
  37. display: flex;
  38. justify-content: space-between;
  39. align-items: center;
  40. .title {
  41. font-family: PingFang SC, PingFang SC;
  42. font-weight: 400;
  43. font-size: 30rpx;
  44. color: #313960;
  45. }
  46. }
  47. .splitline {
  48. margin-left: -20rpx;
  49. margin-right: -20rpx;
  50. margin-top: 30rpx;
  51. height: 1rpx;
  52. background: #f3f3f3;
  53. }
  54. }
  55. </style>
  56. <!-- 滑块开关按钮的样式 -->
  57. <style scoped="scoped" lang="scss">
  58. .switch {
  59. position: relative;
  60. display: inline-block;
  61. width: 216rpx;
  62. height: 68rpx;
  63. }
  64. .switch input {
  65. opacity: 0;
  66. width: 0;
  67. height: 0;
  68. }
  69. .slider {
  70. position: absolute;
  71. top: 0;
  72. left: 0;
  73. right: 0;
  74. bottom: 0;
  75. background: #F1F4F5;
  76. border-radius: 8rpx 8rpx 8rpx 8rpx;
  77. -webkit-transition: .4s;
  78. transition: .4s;
  79. }
  80. .swtichTag {
  81. position: absolute;
  82. height: 52rpx;
  83. width: 92rpx;
  84. left: 8rpx;
  85. text-align: center;
  86. line-height: 52rpx;
  87. bottom: 8rpx;
  88. background-color: white;
  89. border-radius: 8rpx 8rpx 8rpx 8rpx;
  90. -webkit-transition: .4s;
  91. transition: .4s;
  92. }
  93. .sliderRun {
  94. // 选中状态下滑块往右边移动
  95. .swtichTag {
  96. -webkit-transform: translateX(108rpx);
  97. -ms-transform: translateX(108rpx);
  98. transform: translateX(108rpx);
  99. }
  100. /* 选中状态下 左边的文字位置与样式 */
  101. .lefTag {
  102. color: #313960;
  103. }
  104. /* 选中状态下 右边的文字位置与样式 */
  105. .rightTag {
  106. color: #5EC6A7;
  107. }
  108. }
  109. /* 默认状态下左边的文字位置与样式 */
  110. .lefTag {
  111. position: absolute;
  112. left: 24rpx;
  113. top: 12rpx;
  114. font-size: 30rpx;
  115. color: #5EC6A7;
  116. z-index: 999;
  117. }
  118. /* 默认状态下右边的文字位置与样式 */
  119. .rightTag {
  120. position: absolute;
  121. right: 26rpx;
  122. top: 12rpx;
  123. font-size: 30rpx;
  124. color: #313960;
  125. z-index: 999;
  126. }
  127. </style>

欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价

css弹性盒子flex布局

css弹性盒子由于版本不同浏览器问题造成了一些不同的写法display:flexbox;在google浏览器中如果使用下面的写法就不行displa...

css图片和文字对齐问题

文字和图片写到一排经常会出现对不齐的问题 这样感觉图片会上来一点没有和文字对齐,如下图 但是如果修改下html结...

css实现简单矩形对话框

在前端做项目时,我们可能会遇到写对话框的需求,这次做视频会议页面就遇到了,记录下日后有个参照。//网页部分 &lt;divcla...

如何修改css中存在的element.style内联样式

改腾讯地图的时候调整了下样式,发现样式一直存在问题,修改style里面的值,一点用都没有,html中这个值还找不到是在哪里出...

珍藏!! JS css不传之秘集合

&lt;!--其中5指每隔5秒刷新一次页面--&gt; &lt;metahttp-equiv=&quot;refresh&quot;content=&quot;5&quot;&gt; =========...

css中单位pxemrem和vh/vw的理解

&gt;px像素(Pixel)。相对长度单位。像素px是相对于显示器屏幕分辨率而言的。em是相对长度单位。相对于当前对象内文本的字...

DIV+css网页布局常用的一些基础知识整理

一.文件命名规范全局样式:global.css;框架布局:layout.css;字体样式:font.css;链接样式:link.css;打印样式:print...

css相对定位与绝对定位

一般相对定位和绝对定位可以配合起来使用 例如实现如下的效果 只需要在外层div设置为相对定位,在内部设置为绝对定位就...

修改了css让浏览器从缓存中更新

当我们修改了css后,如果不做一些操作,浏览器是不会自动更新我们的样式文件的。除非是过期或者用户手动刷新清理缓存等。所...

css定位的简单运用

父容器使用相对定位position: relative子容器使用绝对定位position: relative这样就可以子容器相对父容器定位了,可以写一...

jscss异步加载

有些时候为了效率,我们需要js与css能够异步加载方法1:直接在后面加一个async的关键字&lt;scriptsrc=&quot;~/lib/jquery/d...

css3样式学习代码

上代码!上代码!不说了&lt;!DOCTYPEhtml&gt; &lt;htmllang=&quot;en&quot;&gt; &lt;head&gt; &lt;metacharset=&quot;UT...

css实现动画效果案列:冒泡案列

css实现动画效果案列:冒泡案列效果图:代码:&lt;!DOCTYPEhtml&gt; &lt;html&gt; &lt;head&gt; &lt;metacharset=&quot;...

html/css样式

代码: &lt;!DOCTYPEhtml&gt; &lt;html&gt; &lt;head&gt; &lt;metacharset=&quot;utf-8&quot;/&gt; &lt;title&gt...

css div水平居 文字垂直居中 flex的方式实现div的垂直与水平居中布局模板

DIV等元素水平居中定宽度+margin:top auto可以让一个块级别元素在外层居中例如这样:效果:可以看到这个div已经居中了但是...

使用jquery操作元素的css样式(获取、修改等等)

使用jquery操作元素的css样式(获取、修改等等) //1、获取和设置样式 $(&quot;#tow&quot;).attr(&quot;class&quot;)...