Skip to content

自定义样式

本节将详细介绍 SunlightImages 组件的自定义样式功能,包括如何通过 wrapperStyle 属性来自定义组件的外观。

基本用法

通过 wrapperStyle 属性可以自定义组件的样式,包括边框颜色、背景色、阴影等。

示例

自定义样式

自定义组件边框、背景等样式

自定义背景色

深色背景

配置说明

属性名类型默认值说明
wrapperStyleobject{}组件样式配置
wrapperStyle.borderColorstring'#dcdfe6'边框颜色
wrapperStyle.focusBorderColorstring'#409eff'聚焦/悬停时边框颜色
wrapperStyle.focusBoxShadowstringundefined聚焦/悬停时阴影
wrapperStyle.backgroundColorstringundefined背景颜色
wrapperStyle.boxShadowstringundefined阴影
widthstring'150px'组件宽度
heightstring'150px'组件高度
borderRadiusstring'8px'组件边框圆角

示例代码

vue
<template>
  <div class="demo-container">
    <h3>自定义样式</h3>
    <SunlightImages
      v-model:fileList="fileList"
      :api="uploadApi"
      :height="'120px'"
      :width="'120px'"
      :borderRadius="'8px'"
      :wrapperStyle="{
        borderColor: '#c0c4cc',
        focusBorderColor: '#67c23a',
        backgroundColor: '#f0f9eb',
        boxShadow: '0 2px 12px 0 rgba(0, 0, 0, 0.1)',
      }"
    />
    <div class="demo-info">
      <p>当前上传数量: {{ fileList.length }} 张</p>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import SunlightImages from 'sunlight-ui';

const fileList = ref([]);

// 上传 API
const uploadApi = async formData => {
  try {
    // 使用指定的接口 `https://jsonplaceholder.typicode.com/posts/`
    const response = await fetch("https://jsonplaceholder.typicode.com/posts/", {
      method: "POST",
      body: formData,
    });

    // 模拟返回图片URL,实际项目中根据接口返回格式调整
    const data = await response.json();
    return {
      fileUrl: `https://picsum.photos/300/300?random=${data.id}`,
    };
  } catch (error) {
    throw error;
  }
};
</script>

<style scoped>
.demo-container {
  margin-bottom: 30px;
  padding: 20px;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  background-color: #ffffff;
}

.demo-info {
  margin-top: 15px;
  padding: 10px;
  background-color: #f5f7fa;
  border-radius: 4px;
  font-size: 14px;
  color: #606266;
}
</style>

高级用法

多种样式组合

你可以根据需要组合多种样式属性,创建不同风格的上传组件。

示例

多种样式组合

展示不同样式的图片上传组件

默认样式

绿色边框

红色边框

带阴影