javascript网页设计案例

 以下是一个简短的范例,该范例展示了如何使用HTML、CSS和JavaScript创建一个简单的网页,其中包含一个按钮,点击按钮后会显示一条消息。

HTML (index.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Webpage Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <h1>Welcome to My Simple Webpage</h1>
        <button id="myButton">Click Me!</button>
        <p id="message" class="hidden">Hello! You clicked the button!</p>
    </div>

    <script src="script.js"></script>
</body>
</html>

CSS (styles.css)

body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
}

.container {
    text-align: center;
    padding: 20px;
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
}

button {
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 5px;
    transition: background-color 0.3s ease;
}

button:hover {
    background-color: #0056b3;
}

.hidden {
    display: none;
}

#message {
    margin-top: 20px;
    color: #28a745;
    font-size: 18px;
}

JavaScript (script.js)

document.addEventListener('DOMContentLoaded', () => {
    const button = document.getElementById('myButton');
    const message = document.getElementById('message');

    button.addEventListener('click', () => {
        message.classList.toggle('hidden');
    });
});

说明

1. HTML: index.html文件包含基本的结构。
    - 一个标题(`h1`)欢迎用户。
    - 一个按钮(`button`)用于触发消息显示。
    - 一个段落(`p`)显示点击按钮后的消息,初始状态为隐藏。
2. CSS: styles.css文件提供了基本的样式。
    - 使用CSS来居中对齐内容并增加一些样式。
    - 定义 .hidden 类来隐藏消息段落。
    - 添加按钮的悬停效果。
3. JavaScript: script.js文件包含简单的交互逻辑。
    - 使用`DOMContentLoaded`事件确保DOM完全加载。
    - 添加一个按钮点击事件监听器,用于切换消息的显示状态。
这个简单的范例展示了HTML、CSS和JavaScript如何协同工作,创建一个基本的互动网页。可以根据需求扩展和自定义这个基本结构。 

一个简单的JavaScript网页设计案例是创建一个动态计数器。以下是一个简单的HTML和JavaScript代码示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态计数器</title>
</head>
<body>
    <h1>动态计数器</h1>
    <p>点击按钮增加计数:</p>
    <button onclick="incrementCounter()">点击我</button>
    <p id="counter">0</p>

    <script>
        let count = 0;

        function incrementCounter() {
            count++;
            document.getElementById("counter").innerHTML = count;
        }
    </script>
</body>
</html>

在这个例子中,我们创建了一个简单的HTML页面,包含一个标题、一个段落和一个按钮。当用户点击按钮时,会触发incrementCounter函数,该函数会增加计数器的值并更新页面上的显示。

JavaScript在网页设计中有着广泛的应用,以下是一些具体的JavaScript网页设计案例:

一、动态时钟

动态时钟是一个常见的JavaScript应用,它可以在网页上显示当前时间,并且每秒更新一次。以下是一个简单的动态时钟代码示例:

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>Dynamic Clock</title>  
    <style>  
        body {  
            display: flex;  
            justify-content: center;  
            align-items: center;  
            height: 100vh;  
            font-family: Arial, sans-serif;  
        }  
        #clock {  
            font-size: 48px;  
            font-weight: bold;  
        }  
    </style>  
</head>  
<body>  
    <div id="clock"></div>  
    <script>  
        function updateClock() {  
            const clockElement = document.getElementById('clock');  
            const now = new Date();  
            clockElement.textContent = now.toLocaleTimeString();  
        }  
        setInterval(updateClock, 1000);  
        updateClock(); // Initial call to set time right away  
    </script>  
</body>  
</html>

二、图片轮播

图片轮播是另一个常见的JavaScript应用,它可以在网页上循环显示一系列图片。以下是一个简单的图片轮播代码示例:

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>Image Carousel</title>  
    <style>  
        body {  
            display: flex;  
            justify-content: center;  
            align-items: center;  
            height: 100vh;  
            font-family: Arial, sans-serif;  
        }  
        img {  
            width: 500px;  
            height: 300px;  
            object-fit: cover;  
            display: none;  
        }  
        #carousel img.active {  
            display: block;  
        }  
        button {  
            padding: 10px;  
            margin: 5px;  
        }  
    </style>  
</head>  
<body>  
    <div id="carousel">  
        <img src="image1.jpg" class="active">  
        <img src="image2.jpg">  
        <img src="image3.jpg">  
    </div>  
    <button onclick="previousImage()">Previous</button>  
    <button onclick="nextImage()">Next</button>  
    <script>  
        let currentImageIndex = 0;  
        const images = document.querySelectorAll('#carousel img');  
  
        function showImage(index) {  
            images.forEach((img, i) => {  
                img.classList.toggle('active', i === index);  
            });  
        }  
  
        function nextImage() {  
            currentImageIndex = (currentImageIndex + 1) % images.length;  
            showImage(currentImageIndex);  
        }  
  
        function previousImage() {  
            currentImageIndex = (currentImageIndex - 1 + images.length) % images.length;  
            showImage(currentImageIndex);  
        }  
    </script>  
</body>  
</html>

三、下拉菜单

下拉菜单是一个常见的用户界面元素,它可以通过JavaScript实现交互效果。以下是一个简单的下拉菜单代码示例:

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>Dropdown Menu</title>  
    <style>  
        #dropdown {  
            position: relative;  
        }  
        #dropdownButton {  
            padding: 10px;  
            background-color: #4CAF50;  
            color: white;  
            border: none;  
            cursor: pointer;  
        }  
        #dropdownContent {  
            display: none;  
            position: absolute;  
            background-color: #f9f9f9;  
            min-width: 160px;  
            box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);  
            z-index: 1;  
        }  
        #dropdownContent a {  
            padding: 12px 16px;  
            text-decoration: none;  
            display: block;  
        }  
        #dropdown:hover #dropdownContent {  
            display: block;  
        }  
    </style>  
</head>  
<body>  
    <div id="dropdown">  
        <button id="dropdownButton">菜单</button>  
        <div id="dropdownContent">  
            <a href="#">选项 1</a>  
            <a href="#">选项 2</a>  
            <a href="#">选项 3</a>  
        </div>  
    </div>  
    <script>  
        const dropdownButton = document.getElementById('dropdownButton');  
        const dropdownContent = document.getElementById('dropdownContent');  
  
        dropdownButton.addEventListener('click', function() {  
            if (dropdownContent.style.display === 'block') {  
                dropdownContent.style.display = 'none';  
            } else {  
                dropdownContent.style.display = 'block';  
            }  
        });  
    </script>  
</body>  
</html>

四、表单验证

表单验证是JavaScript在网页设计中的一个重要应用,它可以确保用户输入的数据符合特定的格式或要求。以下是一个简单的表单验证代码示例:

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>Form Validation</title>  
    <style>  
        input:invalid {  
            border: 2px solid red;  
        }  
        input:valid {  
            border: 2px solid green;  
        }  
    </style>  
</head>  
<body>  
    <form>  
        <label for="username">用户名:</label><br>  
        <input type="text" id="username" pattern="[a-zA-Z0-9]{3,10}" required><br>  
        <label for="email">电子邮件:</label><br>  
        <input type="email" id="email" required><br>  
        <!-- 可以添加更多的表单元素和验证规则 -->  
        <input type="submit" value="提交">  
    </form>  
</body>  
</html>

五、任务列表应用

任务列表应用是一个更复杂的JavaScript网页设计案例,它展示了如何使用JavaScript处理DOM操作以及实现简单的交互效果。以下是一个简单的任务列表应用代码示例:

<!DOCTYPE html>  
<html lang="zh">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>任务列表</title>  
    <link rel="stylesheet" href="styles.css">  
</head>  
<body>  
    <div class="container">  
        <h1>任务列表</h1>  
        <input type="text" id="taskInput" placeholder="输入任务...">  
        <button id="addTaskBtn">添加任务</button>  
        <ul id="taskList"></ul>  
        <button id="clearCompletedBtn">清空已完成任务</button>  
    </div>  
    <script src="script.js"></script>  
</body>  
</html>

对应的CSS文件(styles.css):

body {  
    font-family: Arial, sans-serif;  
    background-color: #f4f4f4;  
    margin: 0;  
    padding: 0;  
}  
  
.container {  
    max-width: 600px;  
    margin: 50px auto;  
    background: #fff;  
    padding: 20px;  
    border-radius: 8px;  
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);  
}  
  
h1 {  
    text-align: center;  
}  
  
input[type="text"] {  
    width: calc(100% - 130px);  
    padding: 10px;  
    border: 1px solid #ccc;  
    border-radius: 4px;  
}  
  
button {  
    padding: 10px;

以下是一个简单的JavaScript网页设计案例,展示如何创建一个动态的网页,用户可以在页面上点击按钮来改变背景颜色。

HTML部分

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Background Color Changer</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            padding-top: 50px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            margin: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <h1>Background Color Changer</h1>
    <button onclick="changeColor('red')">Red</button>
    <button onclick="changeColor('green')">Green</button>
    <button onclick="changeColor('blue')">Blue</button>
    <button onclick="changeColor('yellow')">Yellow</button>
    <button onclick="changeColor('random')">Random</button>

    <script src="script.js"></script>
</body>
</html>

JavaScript部分 (script.js)

function changeColor(color) {
    if (color === 'random') {
        // Generate a random color
        const randomColor = '#' + Math.floor(Math.random()*16777215).toString(16);
        document.body.style.backgroundColor = randomColor;
    } else {
        document.body.style.backgroundColor = color;
    }
}

解释

  1. HTML部分:

    • 创建了一个简单的网页结构,包含一个标题和五个按钮。

    • 每个按钮都有一个onclick事件,当用户点击按钮时,会调用changeColor函数,并传递一个颜色参数。

  2. JavaScript部分:

    • changeColor函数接收一个颜色参数。

    • 如果参数是'random',则生成一个随机的十六进制颜色代码,并将其应用到页面的背景颜色上。

    • 如果参数是其他颜色(如'red''green''blue''yellow'),则直接将该颜色应用到页面的背景颜色上。

运行效果

  • 当用户点击“Red”按钮时,页面背景会变成红色。

  • 当用户点击“Green”按钮时,页面背景会变成绿色。

  • 当用户点击“Blue”按钮时,页面背景会变成蓝色。

  • 当用户点击“Yellow”按钮时,页面背景会变成黄色。

  • 当用户点击“Random”按钮时,页面背景会变成一个随机的颜色。

这个案例展示了如何使用JavaScript来动态改变网页的背景颜色,是一个简单但实用的网页设计示例。

以下是一个简单的 JavaScript 网页设计案例:

一、页面布局

创建一个基本的 HTML 结构,包含一个标题、一个段落和一个按钮。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript 网页设计案例</title>
</head>

<body>
    <h1>欢迎来到我的网页</h1>
    <p>这是一个使用 JavaScript 实现交互的网页示例。</p>
    <button id="changeColorButton">改变背景颜色</button>
</body>

</html>

二、添加 JavaScript 功能

使用 JavaScript 为按钮添加点击事件,当按钮被点击时,改变页面的背景颜色。

// 获取按钮元素
const changeColorButton = document.getElementById('changeColorButton');

// 添加点击事件监听器
changeColorButton.addEventListener('click', function () {
    const randomColor = getRandomColor();
    document.body.style.backgroundColor = randomColor;
});

// 生成随机颜色的函数
function getRandomColor() {
    const letters = '0123456789ABCDEF';
    let color = '#';
    for (let i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
    }
    return color;
}

在这个案例中,当用户点击 “改变背景颜色” 按钮时,页面的背景颜色会随机变为一种十六进制颜色。你可以根据自己的需求进一步扩展这个案例,添加更多的交互元素和功能。例如,可以添加输入框让用户输入颜色值,或者添加动画效果等。

以下是一个简单的JavaScript网页设计案例,展示了如何使用JavaScript来增强网页的交互性。这个案例将创建一个简单的计算器,用户可以输入两个数字并选择操作(加、减、乘、除),然后点击按钮进行计算。

HTML部分

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Calculator</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="calculator">
        <input type="number" id="num1" placeholder="Enter first number">
        <input type="number" id="num2" placeholder="Enter second number">
        <select id="operation">
            <option value="add">Add</option>
            <option value="subtract">Subtract</option>
            <option value="multiply">Multiply</option>
            <option value="divide">Divide</option>
        </select>
        <button onclick="calculate()">Calculate</button>
        <div id="result"></div>
    </div>
    <script src="script.js"></script>
</body>
</html>

CSS部分(styles.css)

body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f0f0f0;
}

.calculator {
    background-color: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

input[type="number"] {
    width: calc(50% - 10px);
    padding: 10px;
    margin: 5px;
    border: 1px solid #ccc;
    border-radius: 4px;
}

select {
    width: calc(100% - 22px);
    padding: 10px;
    margin: 5px;
    border: 1px solid #ccc;
    border-radius: 4px;
}

button {
    width: 100%;
    padding: 10px;
    border: none;
    background-color: #007BFF;
    color: #fff;
    border-radius: 4px;
    cursor: pointer;
}

button:hover {
    background-color: #0056b3;
}

#result {
    margin-top: 20px;
    font-size: 1.2em;
    color: #333;
}

JavaScript部分(script.js)

function calculate() {
    const num1 = parseFloat(document.getElementById('num1').value);
    const num2 = parseFloat(document.getElementById('num2').value);
    const operation = document.getElementById('operation').value;
    let result;

    switch (operation) {
        case 'add':
            result = num1 + num2;
            break;
        case 'subtract':
            result = num1 - num2;
            break;
        case 'multiply':
            result = num1 * num2;
            break;
        case 'divide':
            if (num2 === 0) {
                result = 'Error: Division by zero';
            } else {
                result = num1 / num2;
            }
            break;
        default:
            result = 'Invalid operation';
    }

    document.getElementById('result').innerText = `Result: ${result}`;
}

解释

  1. HTML部分:创建了一个简单的表单,包含两个输入框用于输入数字,一个下拉菜单用于选择操作,一个按钮用于触发计算,以及一个用于显示结果的div
  2. CSS部分:添加了一些基本的样式,使页面看起来更美观。
  3. JavaScript部分:定义了一个calculate函数,该函数获取用户输入的数字和选择的操作,并根据操作进行相应的计算,最后将结果显示在页面上。

这个案例展示了如何使用JavaScript来处理用户输入并进行简单的计算,同时通过CSS增强了页面的视觉效果。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>慕课网</title> <link href="css/style.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="js/js/myfocus-2.0.1.min.js"></script> <script type="text/javascript"> myFocus.set({ id:'demo', pattern:'mF_liquid'}); </script> </head> <body> <div id="top"> <div id="top_text"> <ul> <li><a href="#">联系我们</a></li> <li><a href="#">加入收藏</a></li> <li><a href="#">设为首页</a></li> </ul> </div> </div> <div id="middle"> <div id="logo"> <div id="logo_left"> <img src="images/logo.jpg" alt="" width="200" height="80" /> </div> <div id="logo_right"> <img src="images/tel.jpg" width="28" height="28" /> 24小时服务热线 <span>123-456-7890</span> </div> <br class="clear_float"> </div> <div id="nav"> <div id="nav_left"> <img src="images/nav_left.jpg" width="10" height="40" /> </div> <div id="nav_middle"> <ul> <li><a href="#">首页</a></li> <li><a href="#">关于幕课</a></li> <li><a href="#">新闻动态</a></li> <li><a href="#">课程中心</a></li> <li><a href="#">在线课程</a></li> <li><a href="#">人才招聘</a></li> </ul> <div id="gezi"> <form> <input type="text" /> </form> </div> </div> <div id="nav_right"> <img src="images/nav_right.jpg" width="10" height="40" /> </div> </div> <div id="demo" class="ad"> <div class="pic"> <ul> <li><a href="#"><img src="images/ad2.jpg" thumb="" alt="" text="详细描述2" /></a></li> <li><a href="#"><img src="images/ad3.jpg" thumb="" alt="" text="详细描述3" /></a></li> <li><a href="#"><img src="images/ad4.jpg" thumb="" alt="" text="详细描述4" /></a></li> <li><a href="#"><img src="images/ad3.jpg" thumb="" alt="" text="详细描述5" /></a></li> </ul> </div> </div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

109702008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值