Switches.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <template>
  2. <label :class="classObject">
  3. <input
  4. v-model="value"
  5. autocomplete="off"
  6. type="checkbox"
  7. :disabled="disabled"
  8. @click="clickEvent($event)"
  9. />
  10. <div />
  11. <span
  12. v-if="showText === undefined || showText === null || showText === true"
  13. class="vue-switcher__label"
  14. >
  15. <span
  16. v-if="value"
  17. v-text="textTrue"
  18. />
  19. <span
  20. v-if="!value"
  21. v-text="textFalse"
  22. />
  23. </span>
  24. </label>
  25. </template>
  26. <script>
  27. export default {
  28. name: 'Switches', // eslint-disable-line
  29. props: {
  30. // 选择状态, false
  31. 'modelValue':
  32. {
  33. type: Boolean,
  34. default: false,
  35. },
  36. // 禁用, false
  37. 'disabled':
  38. {
  39. type: Boolean,
  40. default: false,
  41. },
  42. // 是否显示文字提示
  43. 'showText':
  44. {
  45. type: Boolean,
  46. default: false,
  47. },
  48. // True状态下的文字
  49. 'textTrueIn':
  50. {
  51. type: String,
  52. default: '',
  53. },
  54. // False状态下的文字
  55. 'textFalseIn':
  56. {
  57. type: String,
  58. default: '',
  59. },
  60. // 显示的颜色default,primary,success,info,warning,danger
  61. 'color':
  62. {
  63. type: String,
  64. default: '',
  65. },
  66. // 大图标
  67. 'typeBold':{
  68. type: Boolean,
  69. default: false,
  70. },
  71. },
  72. emits: ['update:modelValue'],
  73. data: function () {
  74. var tempValue = false;
  75. var colors = 'red';
  76. if (this.modelValue != undefined) {
  77. tempValue = (this.modelValue == 'true' || this.modelValue == true);
  78. if (this.modelValue == 'true' || this.modelValue == true) {
  79. colors = 'green';
  80. } else {
  81. colors = 'red';
  82. }
  83. }
  84. return {
  85. 'value': tempValue,
  86. 'textTrue': (this.textTrueIn == undefined) ? '是' : this.textTrueIn,
  87. 'textFalse': (this.textFalseIn == undefined) ? '否' : this.textFalseIn,
  88. 'theme': 'default',
  89. 'colorCss': colors,
  90. };
  91. },
  92. computed: {
  93. classObject: function () {
  94. var _self = this;
  95. return {
  96. 'vue-switcher': true,
  97. ['vue-switcher--unchecked']: !_self.value,
  98. ['vue-switcher--disabled']: !!_self.disabled,
  99. ['vue-switcher--bold']: !!_self.typeBold,
  100. ['vue-switcher--bold--unchecked']: !!_self.typeBold && !_self.value,
  101. [`vue-switcher-theme--${_self.theme}`]: _self.theme,
  102. [`vue-switcher-color--${_self.colorCss}`]: _self.colorCss,
  103. };
  104. },
  105. },
  106. watch: {
  107. value: function (val, oldVal) { // eslint-disable-line
  108. this.$emit('update:modelValue', val);
  109. },
  110. modelValue: function (val, oldVal) { // eslint-disable-line
  111. if (val != undefined) {
  112. this.value = (val == 'true' || val == true);
  113. if (val == 'true' || val == true) {
  114. this.colorCss = 'green';
  115. } else {
  116. this.colorCss = 'red';
  117. }
  118. }
  119. },
  120. },
  121. methods: {
  122. clickEvent: function (e) {
  123. var clientX = e.clientX;
  124. var elm = $(e.target);
  125. var left = elm.offset().left;
  126. if (clientX - left > 50) {
  127. e.preventDefault();
  128. }
  129. },
  130. },
  131. };
  132. </script>
  133. <style>
  134. .vue-switcher {
  135. position: relative;
  136. display: inline-block;
  137. width: 100px;
  138. height: 26px;
  139. line-height: 26px;
  140. margin-bottom: 0px;
  141. top: 7px;
  142. }
  143. .vue-switcher__label {
  144. position: absolute;
  145. left: 50px;
  146. top: 0px;
  147. height: 26px;
  148. line-height: 26px;
  149. display: block;
  150. font-size: 10px;
  151. margin-bottom: 0px;
  152. }
  153. .vue-switcher input {
  154. position: absolute;
  155. left: 0px;
  156. top: 0px;
  157. width: 50px;
  158. height: 26px;
  159. line-height: 26px;
  160. opacity: 0;
  161. z-index: 1;
  162. cursor: pointer;
  163. margin: 0px !important;
  164. }
  165. .vue-switcher div {
  166. position: absolute;
  167. left: 0px;
  168. top: 8px;
  169. width: 40px;
  170. height: 10px;
  171. border-radius: 30px;
  172. display: -webkit-flex;
  173. display: -ms-flex;
  174. display: flex;
  175. align-items: center;
  176. justify-content: flex-start;
  177. cursor: pointer;
  178. }
  179. .vue-switcher div:after {
  180. position: absolute;
  181. top: -4;
  182. left: 100%;
  183. content: "";
  184. height: 18px;
  185. width: 18px;
  186. border-radius: 100px;
  187. display: block;
  188. transition: all ease 0.3s;
  189. margin-left: -17px;
  190. cursor: pointer;
  191. }
  192. .vue-switcher--unchecked div {
  193. justify-content: flex-end;
  194. }
  195. .vue-switcher--unchecked div:after {
  196. left: 15px;
  197. }
  198. .vue-switcher--disabled div {
  199. opacity: 0.3;
  200. }
  201. .vue-switcher--disabled input {
  202. cursor: not-allowed;
  203. }
  204. .vue-switcher--bold div {
  205. top: 0px;
  206. height: 26px;
  207. width: 50px;
  208. }
  209. .vue-switcher--bold div:after {
  210. margin-left: -22px;
  211. top: 4px;
  212. }
  213. .vue-switcher--bold--unchecked div:after {
  214. left: 26px;
  215. }
  216. .vue-switcher--bold .vue-switcher__label span {
  217. padding-bottom: 7px;
  218. display: inline-block;
  219. }
  220. .vue-switcher-theme--default.vue-switcher-color--default div {
  221. background-color: #b7b7b7;
  222. }
  223. .vue-switcher-theme--default.vue-switcher-color--default div:after {
  224. background-color: #9d9d9d;
  225. }
  226. .vue-switcher-theme--default.vue-switcher-color--default.vue-switcher--unchecked
  227. div {
  228. background-color: #aaa;
  229. }
  230. .vue-switcher-theme--default.vue-switcher-color--default.vue-switcher--unchecked
  231. div:after {
  232. background-color: #c4c4c4;
  233. }
  234. .vue-switcher-theme--default.vue-switcher-color--blue div {
  235. background-color: #77b0c8;
  236. }
  237. .vue-switcher-theme--default.vue-switcher-color--blue div:after {
  238. background-color: #539bb9;
  239. }
  240. .vue-switcher-theme--default.vue-switcher-color--blue.vue-switcher--unchecked
  241. div {
  242. background-color: #c0dae5;
  243. }
  244. .vue-switcher-theme--default.vue-switcher-color--blue.vue-switcher--unchecked
  245. div:after {
  246. background-color: #77b0c8;
  247. }
  248. .vue-switcher-theme--default.vue-switcher-color--red div {
  249. background-color: #c87777;
  250. }
  251. .vue-switcher-theme--default.vue-switcher-color--red div:after {
  252. background-color: #b95353;
  253. }
  254. .vue-switcher-theme--default.vue-switcher-color--red.vue-switcher--unchecked
  255. div {
  256. background-color: #e5c0c0;
  257. }
  258. .vue-switcher-theme--default.vue-switcher-color--red.vue-switcher--unchecked
  259. div:after {
  260. background-color: #c87777;
  261. }
  262. .vue-switcher-theme--default.vue-switcher-color--yellow div {
  263. background-color: #c9c377;
  264. }
  265. .vue-switcher-theme--default.vue-switcher-color--yellow div:after {
  266. background-color: #bab353;
  267. }
  268. .vue-switcher-theme--default.vue-switcher-color--yellow.vue-switcher--unchecked
  269. div {
  270. background-color: #e6e3c0;
  271. }
  272. .vue-switcher-theme--default.vue-switcher-color--yellow.vue-switcher--unchecked
  273. div:after {
  274. background-color: #c9c377;
  275. }
  276. .vue-switcher-theme--default.vue-switcher-color--orange div {
  277. background-color: #c89577;
  278. }
  279. .vue-switcher-theme--default.vue-switcher-color--orange div:after {
  280. background-color: #b97953;
  281. }
  282. .vue-switcher-theme--default.vue-switcher-color--orange.vue-switcher--unchecked
  283. div {
  284. background-color: #e5cec0;
  285. }
  286. .vue-switcher-theme--default.vue-switcher-color--orange.vue-switcher--unchecked
  287. div:after {
  288. background-color: #c89577;
  289. }
  290. .vue-switcher-theme--default.vue-switcher-color--green div {
  291. background-color: #77c88d;
  292. }
  293. .vue-switcher-theme--default.vue-switcher-color--green div:after {
  294. background-color: #53b96e;
  295. }
  296. .vue-switcher-theme--default.vue-switcher-color--green.vue-switcher--unchecked
  297. div {
  298. background-color: #c0e5ca;
  299. }
  300. .vue-switcher-theme--default.vue-switcher-color--green.vue-switcher--unchecked
  301. div:after {
  302. background-color: #77c88d;
  303. }
  304. </style>
  305. <!--<style lang="scss">
  306. /**
  307. * Default
  308. */
  309. $color-default-default: #aaa;
  310. $color-default-green: #53b96e;
  311. $color-default-blue: #539bb9;
  312. $color-default-red: #b95353;
  313. $color-default-orange: #b97953;
  314. $color-default-yellow: #bab353;
  315. $switch-width: 100px;
  316. $switch-height: 26px;
  317. $theme-default-colors: (
  318. default : $color-default-default,
  319. blue : $color-default-blue,
  320. red : $color-default-red,
  321. yellow : $color-default-yellow,
  322. orange : $color-default-orange,
  323. green : $color-default-green
  324. );
  325. .vue-switcher {
  326. position: relative;
  327. display: inline-block;
  328. width: $switch-width;
  329. height: $switch-height;
  330. line-height: $switch-height;
  331. margin-bottom: 0px;
  332. top: 7px;
  333. &__label {
  334. position: absolute;
  335. left: $switch-width * 0.6;
  336. top: 0px;
  337. height: $switch-height;
  338. line-height: $switch-height;
  339. display: block;
  340. font-size: 10px;
  341. margin-bottom: 0px;
  342. }
  343. input {
  344. position: absolute;
  345. left: 0px;
  346. top: 0px;
  347. width: $switch-width * 0.6;
  348. height: $switch-height;
  349. line-height: $switch-height;
  350. opacity: 0;
  351. z-index: 1;
  352. cursor: pointer;
  353. margin: 0px !important;
  354. }
  355. div {
  356. position: absolute;
  357. left: 0px;
  358. top: ($switch-height - 10) * 0.5;
  359. width: $switch-width * 0.4;
  360. height: 10px;
  361. border-radius: 30px;
  362. display: -webkit-flex;
  363. display: -ms-flex;
  364. display: flex;
  365. align-items: center;
  366. justify-content: flex-start;
  367. cursor: pointer;
  368. &:after {
  369. position: absolute;
  370. top: (10 - 18) * 0.5;
  371. left: 100%;
  372. content: '';
  373. height: 18px;
  374. width: 18px;
  375. border-radius: 100px;
  376. display: block;
  377. transition: all ease .3s;
  378. margin-left: -17px;
  379. cursor: pointer;
  380. }
  381. }
  382. &--unchecked {
  383. div {
  384. justify-content: flex-end;
  385. &:after {
  386. left: 15px;
  387. }
  388. }
  389. }
  390. &--disabled {
  391. div {
  392. opacity: .3;
  393. }
  394. input {
  395. cursor: not-allowed;
  396. }
  397. }
  398. &--bold {
  399. div {
  400. top: ($switch-height - 26) * 0.5;
  401. height: $switch-height;
  402. width: 51px;
  403. &:after {
  404. margin-left: -22px;
  405. top: 4px;
  406. }
  407. }
  408. &--unchecked {
  409. div {
  410. &:after {
  411. left: 26px;
  412. }
  413. }
  414. }
  415. .vue-switcher__label {
  416. span {
  417. padding-bottom: 7px;
  418. display: inline-block;
  419. }
  420. }
  421. }
  422. &-theme--default {
  423. @each $colorName, $color in $theme-default-colors {
  424. &.vue-switcher-color--#{$colorName} {
  425. div {
  426. @if $colorName == 'default' {
  427. background-color: lighten($color, 5%);
  428. } @else {
  429. background-color: lighten($color, 10%);
  430. }
  431. &:after {
  432. @if $colorName == 'default' {
  433. background-color: darken($color, 5%);
  434. } @else {
  435. background-color: $color
  436. }
  437. }
  438. }
  439. &.vue-switcher--unchecked {
  440. div {
  441. @if $colorName == 'default' {
  442. background-color: $color;
  443. } @else {
  444. background-color: lighten($color, 30%);
  445. }
  446. &:after {
  447. background-color: lighten($color, 10%);
  448. }
  449. }
  450. }
  451. }
  452. }
  453. }
  454. }
  455. </style>-->