以下是一个使用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>当前时间: <span id="clock">span>h1>
<script>
function updateClock() {
const now = new Date();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
const formattedTime = `${hours}:${minutes}:${seconds}`;
document.getElementById('clock').innerHTML = formattedTime;
}
setInterval(updateClock, 1000);
script>
body>
html>
案例描述:
创建一个网页,用于展示多张图片,并通过JavaScript实现图片的动态切换展示功能。当用户鼠标悬停在某个图片上时,该图片会放大,并显示相应的标题和描述信息。
案例步骤:
DOCTYPE html>
<html>
<head>
<title>动态图片切换展示title>
<script src="main.js">script>
<link rel="stylesheet" href="styles.css">
head>
<body>
<div id="gallery">
<div class="picture" onmouseover="showInfo(0)" onmouseout="hideInfo()">
<img src="image1.jpg">
<div class="info">
<h2>图片1h2>
<p>这是图片1的描述信息p>
div>
div>
<div class="picture" onmouseover="showInfo(1)" onmouseout="hideInfo()">
<img src="image2.jpg">
<div class="info">
<h2>图片2h2>
<p>这是图片2的描述信息p>
div>
div>
<div class="picture" onmouseover="showInfo(2)" onmouseout="hideInfo()">
<img src="image3.jpg">
<div class="info">
<h2>图片3h2>
<p>这是图片3的描述信息p>
div>
div>
div>
body>
html>
#gallery {
display: flex;
flex-wrap: wrap;
}
.picture {
position: relative;
width: 200px;
height: 200px;
margin: 10px;
overflow: hidden;
}
.picture img {
width: 100%;
height: 100%;
object-fit: cover;
transition: transform 0.5s;
}
.picture:hover img {
transform: scale(1.1);
}
.info {
position: absolute;
bottom: 0;
width: 100%;
background-color: rgba(0, 0, 0, 0.7);
color: #fff;
padding: 10px;
transform: translateY(100%);
transition: transform 0.5s;
}
.picture:hover .info {
transform: translateY(0);
}
function showInfo(index) {
var infoDiv = document.getElementsByClassName('info')[index];
infoDiv.style.transform = 'translateY(0)';
}
function hideInfo() {
var infoDivs = document.getElementsByClassName('info');
for (var i = 0; i < infoDivs.length; i++) {
infoDivs[i].style.transform = 'translateY(100%)';
}
}
案例效果:
用户打开网页后,会看到三张图片的网格布局。当用户鼠标悬停在某个图片上时,该图片会放大,并显示相应的标题和描述信息。当用户移开鼠标时,标题和描述信息会隐藏。
此案例通过JavaScript实现了图片的动态切换展示功能,提升了网页的交互性和用户体验。用户可以通过鼠标悬停来查看图片详细信息,为图片增加了一定的互动性。
表单验证是确保用户输入数据准确性和有效性的重要手段。以下是一个简单的表单验证案例:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>表单验证title>
<style>
input:invalid {
border: 2px solid red;
}
input:valid {
border: 2px solid green;
}
style>
head>
<body>
<form onsubmit="return validateForm()">
<label for="email">电子邮件:label><br>
<input type="email" id="email" required><br>
<button type="submit">提交button>
form>
<script>
function validateForm() {
const email = document.getElementById('email').value;
const emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
if (!emailPattern.test(email)) {
alert("无效的电子邮件地址");
return false;
}
return true;
}
script>
body>
html>
响应式导航栏是一种能够适应不同屏幕尺寸的导航菜单。以下是一个简单的响应式导航栏案例:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>响应式导航栏title>
<style>
.nav {
display: flex;
justify-content: space-between;
}
.nav ul {
list-style: none;
display: flex;
}
.nav li {
margin: 0 10px;
}
@media (max-width: 768px) {
.nav ul {
display: none;
}
.nav button {
display: block;
}
}
style>
head>
<body>
<div class="nav">
<button onclick="toggleNav()">☰ Menubutton>
<ul id="navList">
<li>Homeli>
<li>Aboutli>
<li>Contactli>
ul>
div>
<script>
function toggleNav() {
const navList = document.getElementById('navList');
if (navList.style.display === 'none') {
navList.style.display = 'flex';
} else {
navList.style.display = 'none';
}
}
script>
body>
html>
交互式地图是一种常见的网页设计元素,可以用于展示地理位置信息、导航等。以下是一个使用OpenLayers库创建交互式地图的案例:
DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.15.1/css/ol.css" />
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.15.1/build/ol.js">script>
<title>交互式地图title>
head>
<body>
<div id="map" style="height: 800px;">div>
<script>
// 创建地图
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: ol.proj.fromLonLat([0, 0]),
zoom: 2
})
});
// 添加交互功能
map.addInteraction(new ol.interaction.DragRotateAndZoom());
map.addInteraction(new ol.interaction.MouseWheelZoom());
script>
body>
html>