captcha.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <div class="captchaBox">
  3. <img :src="captcha" alt="验证码加载失败,点击刷新" @load="loadedImage" @error="imageErrorHandle">
  4. <div class="captchaFull refresh" @click="refreshCaptcha" v-show="!loading">点击刷新验证码</div>
  5. <div class="captchaFull image-loading" v-show="loading">加载中...</div>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. name: "captcha",
  11. props: {
  12. captchaUrl: {default:''},
  13. },
  14. data(){
  15. return {
  16. captcha:'',
  17. loading: true,
  18. }
  19. },
  20. mounted() {
  21. this.captcha = this.captchaUrl;
  22. },
  23. methods:{
  24. refreshCaptcha(){
  25. this.loading = true;
  26. this.captcha = this.captchaUrl + `?k1=${Math.random()}&k2=${Math.random()}`
  27. },
  28. loadedImage(){
  29. console.log('图片加载完毕')
  30. this.loading = false;
  31. },
  32. imageErrorHandle(){
  33. this.loading = false;
  34. }
  35. }
  36. }
  37. </script>
  38. <style scoped>
  39. .captchaBox{
  40. position: relative;
  41. border: 1px solid whitesmoke;
  42. background-color: #fff;
  43. }
  44. .captchaBox > img{
  45. width: 100%;
  46. height: 100%;
  47. }
  48. .captchaFull{
  49. position: absolute;
  50. left: 0;
  51. top: 0;
  52. width: 100%;
  53. height: 100%;
  54. display: flex;
  55. justify-content: center;
  56. align-items: center;
  57. color: white;
  58. background-color: rgba(0,0,0,0.3);
  59. transition: opacity 0.7s;
  60. cursor: pointer;
  61. user-select:none;
  62. }
  63. .refresh{
  64. opacity: 0;
  65. }
  66. .refresh:hover{
  67. opacity: 1;
  68. }
  69. </style>