学习写了个域名展示的单页,实现每次重开网页都会换颜色。


预览效果:

1
2
展示地址:https://bipu.net/mi

网页源码:

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="https://bipu.net/usr/uploads/img/icon.ico">
    <title>域名展示</title>
    <style>
        :root {
            --domain-colors: #3f51b5, #4caf50, #f44336, #2196f3, #ffeb3b, #9c27b0, #e91e63, #00bcd4, #cddc39, #673ab7;
        }

        .domain-item {
            padding: 1rem;
            border-radius: ½rem;
            margin-bottom: 1rem;
            display: flex;
            align-items: center;
            justify-content: space-between;
            font-family: Arial, sans-serif;
            box-shadow: 0px .png1px 10px rgba(0, 0, 0, 0.1);
        width:-70%;

                
        }

        .domain-name {
            font-size: 1.2rem;
            font-weight: bold;
        }

        .domain-price {
            color: #ffffff;
            font-size: 1.2rem;
            font-weight: bold
        }
    </style>
    <script>
        function randomColor() {
            const colors = getComputedStyle(document.documentElement).getPropertyValue('--domain-colors').split(', ');
            return colors[Math.floor(Math.random() * colors.length)];
        }

        document.addEventListener('DOMContentLoaded', () => {
            const domainList = [
                { name: 'UU.KE', price: '¥899' },
                { name: 'TDL.CC', price: '¥120' },
                { name: 'FCD.CC', price: '¥120' },
                { name: 'HXXG.CN', price: '¥99' },
                
            ];

            const container = document.getElementById('domainContainer');
            domainList.forEach(domain => {
                const item = document.createElement('div');
                item.className = 'domain-item';
                item.style.backgroundColor = randomColor();
                item.innerHTML = `
                    <span class="domain-name">${domain.name}</span>
                    <span class="domain-price">${domain.price}</span>
                `;
                container.appendChild(item);
            });
        });
    </script>
</head>
<body>
    <h1 style="text-align: center;">域名列表</h1>
    <div id="domainContainer"></div>
</body>
</html>

很简单就实现我需要的功能,边学边练手看起来略显粗糙,优化的空间还很大。颜色比较单调重复,不过就先这样了。在老电脑上用文本文档写的,懒得再打开改。