当前位置:   article > 正文

动手吧,vue单选框_vue的单选框

vue的单选框

单选框到处可见,组件库不方便自定义样式,还是自己写吧。

效果图:

1、template部分

  1. <template>
  2. <label
  3. class="v-radio flex"
  4. :class="[{ disable: disabled }]"
  5. :aria-disabled="disabled"
  6. >
  7. <span
  8. class="v-radio_input flex"
  9. :class="{
  10. disable: disabled,
  11. checked: model === label,
  12. }"
  13. >
  14. <span class="v-radio_inner"></span>
  15. <input
  16. ref="radio"
  17. class="v-radio_original"
  18. :name="name"
  19. :value="label"
  20. v-model="model"
  21. :disabled="disabled"
  22. :aria-disabled="disabled"
  23. @change="handleChange"
  24. type="radio"
  25. />
  26. </span>
  27. <span>
  28. <template v-if="!$slots.default">{{ label }}</template>
  29. <slot></slot>
  30. </span>
  31. </label>
  32. </template>

2、js部分

  1. export default {
  2. data() {
  3. return {};
  4. },
  5. props: {
  6. value: {},
  7. label: {},
  8. name: String,
  9. disabled: {
  10. type: Boolean,
  11. default: false,
  12. },
  13. },
  14. computed: {
  15. model: {
  16. get() {
  17. return this.value;
  18. },
  19. set(val) {
  20. this.$emit("input", val);
  21. },
  22. },
  23. },
  24. methods: {
  25. handleChange() {
  26. this.$nextTick(() => {
  27. let defaultSlots = this.$slots.default[0];
  28. let context = "";
  29. if (defaultSlots.text) {
  30. context = defaultSlots.text;
  31. } else {
  32. context = defaultSlots;
  33. }
  34. this.$emit("change", {
  35. value: this.model,
  36. label: this.label,
  37. context,
  38. });
  39. });
  40. },
  41. },
  42. };

3、css部分

点个赞嘛,不点赞的话打你哦!

  1. .flex {
  2. display: flex;
  3. align-items: center;
  4. }
  5. .v-radio {
  6. cursor: pointer;
  7. margin-right: 28px;
  8. &.disable {
  9. color: #ccc;
  10. cursor: not-allowed;
  11. }
  12. .v-radio_input {
  13. position: relative;
  14. border: 1px solid #ff5858;
  15. border-radius: 50%;
  16. margin-right: 8px;
  17. transition: 0.3s;
  18. &.disable {
  19. border-color: #dad3d3;
  20. }
  21. &::before {
  22. position: absolute;
  23. top: 50%;
  24. left: 50%;
  25. content: "";
  26. width: 8px;
  27. height: 8px;
  28. transform: translate(-50%, -50%);
  29. border-radius: 50%;
  30. background-color: #fff;
  31. }
  32. &.checked {
  33. background: #ff5858;
  34. }
  35. .v-radio_inner {
  36. width: 16px;
  37. height: 16px;
  38. border-radius: 50%;
  39. }
  40. .v-radio_original {
  41. opacity: 0;
  42. position: absolute;
  43. top: 0;
  44. left: 0;
  45. right: 0;
  46. bottom: 0;
  47. z-index: -1;
  48. }
  49. }
  50. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/117982
推荐阅读
相关标签
  

闽ICP备14008679号