neil-modal.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <view class="neil-modal" @touchmove.stop.prevent="bindTouchmove" :class="{'neil-modal--show':isOpen}">
  3. <view class="neil-modal__mask" @click="clickMask"></view>
  4. <view class="neil-modal__container">
  5. <view class="neil-modal__header" v-if="title.length > 0">{{title}}</view>
  6. <view class="neil-modal__content" :class="content ? 'neil-modal--padding' : ''" :style="{textAlign:align}">
  7. <template v-if="content">
  8. <text class="modal-content">{{content}}</text>
  9. </template>
  10. <template v-else>
  11. <slot />
  12. </template>
  13. </view>
  14. <view class="neil-modal__footer">
  15. <view v-if="showCancel" class="neil-modal__footer-left" @click="clickLeft" :style="{color:cancelColor}"
  16. hover-class="neil-modal__footer-hover" :hover-start-time="20" :hover-stay-time="70">
  17. {{cancelText}}
  18. </view>
  19. <view v-if="showConfirm" class="neil-modal__footer-right" @click="clickRight" :style="{color:confirmColor}" hover-class="neil-modal__footer-hover"
  20. :hover-start-time="20" :hover-stay-time="70">
  21. {{confirmText}}
  22. </view>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. export default {
  29. name: 'neil-modal',
  30. props: {
  31. title: { //标题
  32. type: String,
  33. default: ''
  34. },
  35. content: String, //提示的内容
  36. align: { //content 的对齐方式left/center/right
  37. type: String,
  38. default: 'left'
  39. },
  40. cancelText: { //取消按钮的文字,默认为"取消"
  41. type: String,
  42. default: '取消'
  43. },
  44. cancelColor: { //取消按钮颜色
  45. type: String,
  46. default: '#333333'
  47. },
  48. confirmText: { //确定按钮的文字,默认为"确定"
  49. type: String,
  50. default: '确定'
  51. },
  52. confirmColor: { //确认按钮颜色
  53. type: String,
  54. default: '#007aff'
  55. },
  56. showCancel: { //是否显示取消按钮,默认为 true
  57. type: [Boolean, String],
  58. default: false
  59. },
  60. showConfirm: { //是否显示取消按钮,默认为 true
  61. type: [Boolean, String],
  62. default: false
  63. },
  64. show: { //是否显示模态框
  65. type: [Boolean, String],
  66. default: false
  67. },
  68. autoClose: { //点击遮罩是否自动关闭弹窗
  69. type: [Boolean, String],
  70. default: false
  71. }
  72. },
  73. data() {
  74. return {
  75. isOpen: false
  76. }
  77. },
  78. watch: {
  79. show(val) {
  80. this.isOpen = val
  81. }
  82. },
  83. created() {
  84. this.isOpen = this.show
  85. },
  86. methods: {
  87. bindTouchmove() {},
  88. clickLeft() {
  89. setTimeout(() => {
  90. this.$emit('cancel')
  91. }, 200)
  92. this.closeModal()
  93. },
  94. clickRight() {
  95. setTimeout(() => {
  96. this.$emit('confirm')
  97. }, 200)
  98. // this.closeModal()
  99. },
  100. clickMask(){
  101. if(this.autoClose){
  102. this.closeModal()
  103. }
  104. },
  105. closeModal() {
  106. this.showAnimation = false
  107. this.isOpen = false
  108. this.$emit('close')
  109. }
  110. }
  111. }
  112. </script>
  113. <style lang="scss">
  114. $bg-color-mask:rgba(0, 0, 0, 0.5); //遮罩颜色
  115. $bg-color-hover:#f1f1f1; //点击状态颜色
  116. .neil-modal {
  117. position: fixed;
  118. visibility: hidden;
  119. width: 100%;
  120. height: 100%;
  121. top: 0;
  122. left: 0;
  123. z-index: 1000;
  124. transition:visibility 200ms ease-in;
  125. &.neil-modal--show{
  126. visibility: visible;
  127. }
  128. &__header {
  129. position: relative;
  130. overflow: hidden;
  131. text-overflow: ellipsis;
  132. white-space: nowrap;
  133. padding: 18upx 24upx;
  134. line-height: 1.5;
  135. color: #333;
  136. font-size: 32upx;
  137. text-align: center;
  138. &::after {
  139. content: " ";
  140. position: absolute;
  141. left: 0;
  142. bottom: 0;
  143. right: 0;
  144. height: 1px;
  145. border-top: 1px solid #e5e5e5;
  146. transform-origin: 0 0;
  147. transform: scaleY(.5);
  148. }
  149. }
  150. &__container {
  151. position: absolute;
  152. z-index: 999;
  153. top: 50%;
  154. left: 50%;
  155. transform: translate(-50%, -50%) ;
  156. transition: transform 0.3s;
  157. width: 540upx;
  158. border-radius: 20upx;
  159. background-color: #fff;
  160. overflow: hidden;
  161. opacity: 0;
  162. transition: opacity 200ms ease-in;
  163. }
  164. &__content {
  165. position: relative;
  166. color: #333;
  167. font-size: 28upx;
  168. box-sizing: border-box;
  169. line-height: 1.5;
  170. &::after {
  171. content: " ";
  172. position: absolute;
  173. left: 0;
  174. bottom: -1px;
  175. right: 0;
  176. height: 1px;
  177. border-bottom: 1px solid #e5e5e5;
  178. transform-origin: 0 0;
  179. transform: scaleY(.5);
  180. }
  181. }
  182. &__footer {
  183. position: relative;
  184. overflow: hidden;
  185. text-overflow: ellipsis;
  186. white-space: nowrap;
  187. color: #333;
  188. font-size: 32upx;
  189. display: flex;
  190. flex-direction: row;
  191. &-left,
  192. &-right {
  193. position: relative;
  194. flex: 1;
  195. overflow: hidden;
  196. text-overflow: ellipsis;
  197. white-space: nowrap;
  198. height: 88upx;
  199. font-size: 28upx;
  200. line-height: 88upx;
  201. text-align: center;
  202. background-color: #fff;
  203. color: #333;
  204. }
  205. &-right {
  206. color: #007aff;
  207. }
  208. &-left::after {
  209. content: " ";
  210. position: absolute;
  211. right: -1px;
  212. top: 0;
  213. width: 1px;
  214. bottom: 0;
  215. border-right: 1px solid #e5e5e5;
  216. transform-origin: 0 0;
  217. transform: scaleX(.5);
  218. }
  219. &-hover {
  220. background-color: $bg-color-hover;
  221. }
  222. }
  223. &__mask {
  224. display: block;
  225. position: absolute;
  226. z-index: 998;
  227. top: 0;
  228. left: 0;
  229. width: 100%;
  230. height: 100%;
  231. background: $bg-color-mask;
  232. opacity: 0;
  233. transition: opacity 200ms ease-in;
  234. &.neil-modal--show{
  235. opacity: 1;
  236. }
  237. }
  238. &--padding {
  239. padding: 32upx 24upx;
  240. min-height: 90upx;
  241. }
  242. &--show {
  243. .neil-modal__container,.neil-modal__mask{
  244. opacity: 1;
  245. }
  246. }
  247. }
  248. </style>