background-image.uvue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex:1">
  4. <!-- #endif -->
  5. <view>
  6. <!-- 测试iOS平台宽高为0时,设置渐变色会不会导致闪退 -->
  7. <view style="width: 0px; height: 0px; background-image: linear-gradient(to bottom,#f5f5f5,#eff2f5);"></view>
  8. <text>不支持背景图片,仅支持linear-gradient方法</text>
  9. <view v-for="(direction) in directionData">
  10. <text>background-image: linear-gradient({{direction}}, red, yellow)</text>
  11. <view class="common"
  12. :style="{'background-image': backgroundSelect ?'linear-gradient('+direction+', red, yellow)':''}"></view>
  13. </view>
  14. <view>
  15. <text>style 动态切换 background</text>
  16. <view @click='changeBg' class="common" :style='testStyle'></view>
  17. </view>
  18. </view>
  19. <!-- #ifdef APP -->
  20. </scroll-view>
  21. <!-- #endif -->
  22. </template>
  23. <script>
  24. export default {
  25. data() {
  26. return {
  27. backgroundSelect: true,
  28. directionData: ['to right', 'to left', 'to bottom', 'to top', 'to bottom left', 'to bottom right', 'to top left', 'to top right'],
  29. testStyle: "background:linear-gradient(to right, red, yellow)"
  30. }
  31. },
  32. methods: {
  33. //供自动化测试使用
  34. updateBackgroundSelect() {
  35. this.backgroundSelect = !this.backgroundSelect
  36. },
  37. changeBg() {
  38. const isColor = this.testStyle == "background:blue"
  39. if (isColor) {
  40. this.setBackgroundImage()
  41. } else {
  42. this.setBackgroundColor()
  43. }
  44. },
  45. setBackgroundColor() {
  46. this.testStyle = "background:blue"
  47. },
  48. setBackgroundImage() {
  49. this.testStyle = "background:linear-gradient(to right, red, yellow)"
  50. }
  51. }
  52. }
  53. </script>
  54. <style>
  55. .common {
  56. width: 250px;
  57. height: 250px;
  58. }
  59. </style>