当前位置: 首页 > news >正文

html | 预览一个颜色数组

Aim: 查看几个颜色的效果,在网页中使用

1.效果1

xx
<script>
function displayHexColors(hexColors, barWidth = 100, barHeight = 100, textPosition = 'right') {
  // Create a container to hold the color bars
  const container = document.createElement('div');
  container.style.display = 'flex';
  container.style.flexDirection = 'column';
  container.style.gap = '0';

  // Create and append color bars to the container
  hexColors.forEach(hex => {
    const colorBar = document.createElement('div');
    const colorText = document.createElement('div');

    // Style the color bar
    colorBar.style.backgroundColor = hex;
    colorBar.style.width = `${barWidth}px`;
    colorBar.style.height = `${barHeight}px`;
    colorBar.style.margin = '0';
    colorBar.style.position = 'relative';
    colorBar.style.display = 'flex';
    colorBar.style.alignItems = 'center';
    colorBar.style.justifyContent = textPosition === 'left' ? 'flex-start' : 'flex-end';

    // Style the text container
    colorText.textContent = hex;
    colorText.style.color = 'white';
    colorText.style.fontSize = '12px';
    colorText.style.fontFamily = 'Arial, sans-serif';
    colorText.style.background = 'rgba(0,0,0,0.5)';
    colorText.style.padding = '2px 3px';
    colorText.style.borderRadius = '3px';
    colorText.style.margin = textPosition === 'left' ? '0 0 0 5px' : '0 5px 0 0';

    // Append text to the color bar
    colorBar.appendChild(colorText);

    // Append the color bar to the container
    container.appendChild(colorBar);
  });

  // Append the container to the body or a specific element
  document.body.appendChild(container);
}

// Example usage:
const hexColors = ["#E64B35","#4DBBD5","#00A087","#3C5488","#F39B7F","#8491B4","#91D1C2","#DC0000","#7E6148","#B09C85","#F2AF1C","#668C13"];
displayHexColors(hexColors, 300, 30, 'right'); // Display in column with width 300px and height 50px, text on the right

displayHexColors(hexColors, 300, 50, 'left');  // Display in column with width 300px and height 50px, text on the left
</script>

在这里插入图片描述

2. 效果2

xx
<script>
function displayHexColors(hexColors, config) {
  // Destructure config object
  const { barWidth = 100, barHeight = 100, textPosition = 'right' } = config;

  // Create a container to hold the color bars
  const container = document.createElement('div');
  container.style.display = 'block';
  container.style.flexDirection = 'column';
  container.style.flexWrap = 'wrap';
  container.style.gap = '0';

  // Create and append color bars to the container
  hexColors.forEach(hex => {
    const colorBarWrapper = document.createElement('div');
    const colorBar = document.createElement('div');
    const colorText = document.createElement('div');

    // Style the color bar wrapper
    colorBarWrapper.style.display = 'flex';
    colorBarWrapper.style.alignItems = 'center';
    colorBarWrapper.style.margin = '0';

    // Style the color bar
    colorBar.style.backgroundColor = hex;
    colorBar.style.width = `${barWidth}px`;
    colorBar.style.height = `${barHeight}px`;
    colorBar.style.margin = '0';

    // Style the text container
    colorText.textContent = hex;
    colorText.style.color = 'black';
    colorText.style.fontSize = '12px';
    colorText.style.fontFamily = 'Arial, sans-serif';
    colorText.style.background = 'rgba(255,255,255,0.8)';
    colorText.style.padding = '2px 1px';
    colorText.style.borderRadius = '3px';
    colorText.style.marginLeft = textPosition === 'left' ? '5px' : '0';
    colorText.style.marginRight = textPosition === 'right' ? '5px' : '0';

    // Position the text outside the color bar
    if (textPosition === 'left') {
      colorBarWrapper.appendChild(colorText);
      colorBarWrapper.appendChild(colorBar);
    } else if (textPosition === 'center') {
      colorText.style.position = 'absolute';
      colorText.style.left = '50%';
      colorText.style.transform = 'translateX(-50%)';
      colorText.style.top = `-${colorText.offsetHeight + 5}px`;
      colorBarWrapper.appendChild(colorText);
      colorBarWrapper.appendChild(colorBar);
    } else {
      colorBarWrapper.appendChild(colorBar);
      colorBarWrapper.appendChild(colorText);
    }

    // Append the color bar wrapper to the container
    container.appendChild(colorBarWrapper);
  });

  // Append the container to the body or a specific element
  document.body.appendChild(container);
}

// Example usage:
//hexColors=['#FF5733', '#33FF57', '#3357FF', '#F1C40F', '#8E44AD']
hexColors=["#E64B35","#4DBBD5","#00A087","#3C5488","#F39B7F","#8491B4","#91D1C2","#DC0000","#7E6148","#B09C85","#F2AF1C","#668C13"]

const config1 = {
  barWidth: 100,
  barHeight: 25,
  textPosition: 'right'
};

const config2 = {
  barWidth: 50,
  barHeight: 20,
  textPosition: 'left'
};

displayHexColors(hexColors, config1);   // Display each color in a separate row
displayHexColors(hexColors, config2);   // Display all colors in a single line
</script>

在这里插入图片描述

相关文章:

  • OpenHarmony文件管理子系统
  • Kubernetes集群部署实战:从零到英雄
  • 【Flutter】正方形的Dialog
  • 通俗易懂的聚类算法之K均值详解
  • 嵌入式开发:磁通门传感器开发(4):自然环境中的磁场
  • Pycharm配置ROS开发环境
  • 《Docker 核心概念揭秘:如何让软件开发像烹饪一样简单》
  • 【前端进阶】14 提升编程体验:组件化与模块化
  • 随机选择文件,向后写入文件内容
  • 04_DeepLearning_SVM
  • day3作业
  • 队列的顺序结构—循环队列的判断条件(rear + 1) % MAXSIZE分析
  • intra-mart实现logicDesigner与forma联动
  • AI编程工具-(四)
  • 五种经典算法路径规划—遗传算法、麻雀算法、狼群优化、粒子群算法、差分进化算法(Matlab源码)
  • IO进程线程2
  • maven高级-05.私服
  • 从零开始实现机器臂仿真(UR5)
  • 计算机毕业设计SpringBoot+Vue.js医院挂号就诊系统(源码+文档+PPT+讲解)
  • 【Azure 架构师学习笔记】- Azure Databricks (15) --Delta Lake 和Data Lake
  • 做外贸批发开什么网站/seo网站优化培训要多少钱
  • 百度关键词策划和seo的优化/推推蛙seo
  • 泰安新闻出版小镇/seo快速推广窍门大公开
  • 岳阳网站定制/优化培训方式
  • 企业网站建设与实施调查报告/郑州网站开发顾问
  • 有可以做ssgsea的网站么/千峰培训多少钱