| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- <template>
- <label :class="classObject">
- <input
- v-model="value"
- type="checkbox"
- :disabled="disabled"
- @click="clickEvent($event)"
- />
- <div />
- <span
- v-if="showText === undefined || showText === null || showText === true"
- class="vue-switcher__label"
- >
- <span
- v-if="value"
- v-text="textTrue"
- />
- <span
- v-if="!value"
- v-text="textFalse"
- />
- </span>
- </label>
- </template>
- <script>
- export default {
- name: 'Switches', // eslint-disable-line
- props: {
- // 选择状态, false
- 'modelValue':
- {
- type: Boolean,
- default: false,
- },
- // 禁用, false
- 'disabled':
- {
- type: Boolean,
- default: false,
- },
- // 是否显示文字提示
- 'showText':
- {
- type: Boolean,
- default: false,
- },
- // True状态下的文字
- 'textTrueIn':
- {
- type: String,
- default: '',
- },
- // False状态下的文字
- 'textFalseIn':
- {
- type: String,
- default: '',
- },
- // 显示的颜色default,primary,success,info,warning,danger
- 'color':
- {
- type: String,
- default: '',
- },
- // 大图标
- 'typeBold':{
- type: Boolean,
- default: false,
- },
- },
- emits: ['update:modelValue'],
- data: function () {
- var tempValue = false;
- var colors = 'red';
- if (this.modelValue != undefined) {
- tempValue = (this.modelValue == 'true' || this.modelValue == true);
- if (this.modelValue == 'true' || this.modelValue == true) {
- colors = 'green';
- } else {
- colors = 'red';
- }
- }
- return {
- 'value': tempValue,
- 'textTrue': (this.textTrueIn == undefined) ? '是' : this.textTrueIn,
- 'textFalse': (this.textFalseIn == undefined) ? '否' : this.textFalseIn,
- 'theme': 'default',
- 'colorCss': colors,
- };
- },
- computed: {
- classObject: function () {
- var _self = this;
- return {
- 'vue-switcher': true,
- ['vue-switcher--unchecked']: !_self.value,
- ['vue-switcher--disabled']: !!_self.disabled,
- ['vue-switcher--bold']: !!_self.typeBold,
- ['vue-switcher--bold--unchecked']: !!_self.typeBold && !_self.value,
- [`vue-switcher-theme--${_self.theme}`]: _self.theme,
- [`vue-switcher-color--${_self.colorCss}`]: _self.colorCss,
- };
- },
- },
- watch: {
- value: function (val, oldVal) { // eslint-disable-line
- this.$emit('update:modelValue', val);
- },
- modelValue: function (val, oldVal) { // eslint-disable-line
- if (val != undefined) {
- this.value = (val == 'true' || val == true);
- if (val == 'true' || val == true) {
- this.colorCss = 'green';
- } else {
- this.colorCss = 'red';
- }
- }
- },
- },
- methods: {
- clickEvent: function (e) {
- var clientX = e.clientX;
- var elm = $(e.target);
- var left = elm.offset().left;
- if (clientX - left > 50) {
- e.preventDefault();
- }
- },
- },
- };
- </script>
- <style>
- .vue-switcher {
- position: relative;
- display: inline-block;
- width: 100px;
- height: 26px;
- line-height: 26px;
- margin-bottom: 0px;
- top: 7px;
- }
- .vue-switcher__label {
- position: absolute;
- left: 50px;
- top: 0px;
- height: 26px;
- line-height: 26px;
- display: block;
- font-size: 10px;
- margin-bottom: 0px;
- }
- .vue-switcher input {
- position: absolute;
- left: 0px;
- top: 0px;
- width: 50px;
- height: 26px;
- line-height: 26px;
- opacity: 0;
- z-index: 1;
- cursor: pointer;
- margin: 0px !important;
- }
- .vue-switcher div {
- position: absolute;
- left: 0px;
- top: 8px;
- width: 40px;
- height: 10px;
- border-radius: 30px;
- display: -webkit-flex;
- display: -ms-flex;
- display: flex;
- align-items: center;
- justify-content: flex-start;
- cursor: pointer;
- }
- .vue-switcher div:after {
- position: absolute;
- top: -4;
- left: 100%;
- content: "";
- height: 18px;
- width: 18px;
- border-radius: 100px;
- display: block;
- transition: all ease 0.3s;
- margin-left: -17px;
- cursor: pointer;
- }
- .vue-switcher--unchecked div {
- justify-content: flex-end;
- }
- .vue-switcher--unchecked div:after {
- left: 15px;
- }
- .vue-switcher--disabled div {
- opacity: 0.3;
- }
- .vue-switcher--disabled input {
- cursor: not-allowed;
- }
- .vue-switcher--bold div {
- top: 0px;
- height: 26px;
- width: 50px;
- }
- .vue-switcher--bold div:after {
- margin-left: -22px;
- top: 4px;
- }
- .vue-switcher--bold--unchecked div:after {
- left: 26px;
- }
- .vue-switcher--bold .vue-switcher__label span {
- padding-bottom: 7px;
- display: inline-block;
- }
- .vue-switcher-theme--default.vue-switcher-color--default div {
- background-color: #b7b7b7;
- }
- .vue-switcher-theme--default.vue-switcher-color--default div:after {
- background-color: #9d9d9d;
- }
- .vue-switcher-theme--default.vue-switcher-color--default.vue-switcher--unchecked
- div {
- background-color: #aaa;
- }
- .vue-switcher-theme--default.vue-switcher-color--default.vue-switcher--unchecked
- div:after {
- background-color: #c4c4c4;
- }
- .vue-switcher-theme--default.vue-switcher-color--blue div {
- background-color: #77b0c8;
- }
- .vue-switcher-theme--default.vue-switcher-color--blue div:after {
- background-color: #539bb9;
- }
- .vue-switcher-theme--default.vue-switcher-color--blue.vue-switcher--unchecked
- div {
- background-color: #c0dae5;
- }
- .vue-switcher-theme--default.vue-switcher-color--blue.vue-switcher--unchecked
- div:after {
- background-color: #77b0c8;
- }
- .vue-switcher-theme--default.vue-switcher-color--red div {
- background-color: #c87777;
- }
- .vue-switcher-theme--default.vue-switcher-color--red div:after {
- background-color: #b95353;
- }
- .vue-switcher-theme--default.vue-switcher-color--red.vue-switcher--unchecked
- div {
- background-color: #e5c0c0;
- }
- .vue-switcher-theme--default.vue-switcher-color--red.vue-switcher--unchecked
- div:after {
- background-color: #c87777;
- }
- .vue-switcher-theme--default.vue-switcher-color--yellow div {
- background-color: #c9c377;
- }
- .vue-switcher-theme--default.vue-switcher-color--yellow div:after {
- background-color: #bab353;
- }
- .vue-switcher-theme--default.vue-switcher-color--yellow.vue-switcher--unchecked
- div {
- background-color: #e6e3c0;
- }
- .vue-switcher-theme--default.vue-switcher-color--yellow.vue-switcher--unchecked
- div:after {
- background-color: #c9c377;
- }
- .vue-switcher-theme--default.vue-switcher-color--orange div {
- background-color: #c89577;
- }
- .vue-switcher-theme--default.vue-switcher-color--orange div:after {
- background-color: #b97953;
- }
- .vue-switcher-theme--default.vue-switcher-color--orange.vue-switcher--unchecked
- div {
- background-color: #e5cec0;
- }
- .vue-switcher-theme--default.vue-switcher-color--orange.vue-switcher--unchecked
- div:after {
- background-color: #c89577;
- }
- .vue-switcher-theme--default.vue-switcher-color--green div {
- background-color: #77c88d;
- }
- .vue-switcher-theme--default.vue-switcher-color--green div:after {
- background-color: #53b96e;
- }
- .vue-switcher-theme--default.vue-switcher-color--green.vue-switcher--unchecked
- div {
- background-color: #c0e5ca;
- }
- .vue-switcher-theme--default.vue-switcher-color--green.vue-switcher--unchecked
- div:after {
- background-color: #77c88d;
- }
- </style>
- <!--<style lang="scss">
- /**
- * Default
- */
- $color-default-default: #aaa;
- $color-default-green: #53b96e;
- $color-default-blue: #539bb9;
- $color-default-red: #b95353;
- $color-default-orange: #b97953;
- $color-default-yellow: #bab353;
- $switch-width: 100px;
- $switch-height: 26px;
- $theme-default-colors: (
- default : $color-default-default,
- blue : $color-default-blue,
- red : $color-default-red,
- yellow : $color-default-yellow,
- orange : $color-default-orange,
- green : $color-default-green
- );
- .vue-switcher {
- position: relative;
- display: inline-block;
- width: $switch-width;
- height: $switch-height;
- line-height: $switch-height;
- margin-bottom: 0px;
- top: 7px;
- &__label {
- position: absolute;
- left: $switch-width * 0.6;
- top: 0px;
- height: $switch-height;
- line-height: $switch-height;
- display: block;
- font-size: 10px;
- margin-bottom: 0px;
- }
- input {
- position: absolute;
- left: 0px;
- top: 0px;
- width: $switch-width * 0.6;
- height: $switch-height;
- line-height: $switch-height;
- opacity: 0;
- z-index: 1;
- cursor: pointer;
- margin: 0px !important;
- }
- div {
- position: absolute;
- left: 0px;
- top: ($switch-height - 10) * 0.5;
- width: $switch-width * 0.4;
- height: 10px;
- border-radius: 30px;
- display: -webkit-flex;
- display: -ms-flex;
- display: flex;
- align-items: center;
- justify-content: flex-start;
- cursor: pointer;
- &:after {
- position: absolute;
- top: (10 - 18) * 0.5;
- left: 100%;
- content: '';
- height: 18px;
- width: 18px;
- border-radius: 100px;
- display: block;
- transition: all ease .3s;
- margin-left: -17px;
- cursor: pointer;
- }
- }
- &--unchecked {
- div {
- justify-content: flex-end;
- &:after {
- left: 15px;
- }
- }
- }
- &--disabled {
- div {
- opacity: .3;
- }
- input {
- cursor: not-allowed;
- }
- }
- &--bold {
- div {
- top: ($switch-height - 26) * 0.5;
- height: $switch-height;
- width: 51px;
- &:after {
- margin-left: -22px;
- top: 4px;
- }
- }
- &--unchecked {
- div {
- &:after {
- left: 26px;
- }
- }
- }
- .vue-switcher__label {
- span {
- padding-bottom: 7px;
- display: inline-block;
- }
- }
- }
- &-theme--default {
- @each $colorName, $color in $theme-default-colors {
- &.vue-switcher-color--#{$colorName} {
- div {
- @if $colorName == 'default' {
- background-color: lighten($color, 5%);
- } @else {
- background-color: lighten($color, 10%);
- }
- &:after {
- @if $colorName == 'default' {
- background-color: darken($color, 5%);
- } @else {
- background-color: $color
- }
- }
- }
- &.vue-switcher--unchecked {
- div {
- @if $colorName == 'default' {
- background-color: $color;
- } @else {
- background-color: lighten($color, 30%);
- }
- &:after {
- background-color: lighten($color, 10%);
- }
- }
- }
- }
- }
- }
- }
- </style>-->
|