作为一名Web前端开发新手,掌握一些实用的代码案例对于提升你的技能至关重要。下面,我将为你精选100个实用Web前端代码案例,帮助你更快地入门和实践。
1. 基础页面布局
垂直居中布局
<!DOCTYPE html>
<html>
<head>
<title>垂直居中布局</title>
<style>
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.box {
width: 100px;
height: 100px;
background-color: blue;
}
</style>
</head>
<body>
<div class="container">
<div class="box"></div>
</div>
</body>
</html>
水平居中布局
<!DOCTYPE html>
<html>
<head>
<title>水平居中布局</title>
<style>
.container {
display: flex;
justify-content: center;
height: 100vh;
}
.box {
width: 100px;
height: 100px;
background-color: blue;
}
</style>
</head>
<body>
<div class="container">
<div class="box"></div>
</div>
</body>
</html>
2. CSS3动画效果
CSS3旋转动画
<!DOCTYPE html>
<html>
<head>
<title>CSS3旋转动画</title>
<style>
.box {
width: 100px;
height: 100px;
background-color: red;
animation: rotate 2s infinite;
}
@keyframes rotate {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
CSS3缩放动画
<!DOCTYPE html>
<html>
<head>
<title>CSS3缩放动画</title>
<style>
.box {
width: 100px;
height: 100px;
background-color: blue;
animation: scale 2s infinite;
}
@keyframes scale {
0%, 100% {
transform: scale(1);
}
50% {
transform: scale(1.5);
}
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
3. 响应式布局
使用媒体查询实现响应式布局
<!DOCTYPE html>
<html>
<head>
<title>响应式布局</title>
<style>
.container {
display: flex;
justify-content: space-around;
padding: 10px;
}
.box {
width: 100px;
height: 100px;
background-color: red;
}
@media screen and (max-width: 500px) {
.box {
width: 50px;
height: 50px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</body>
</html>
4. JavaScript常用功能
计算器实现
<!DOCTYPE html>
<html>
<head>
<title>计算器实现</title>
<script>
function calculate() {
var num1 = document.getElementById('num1').value;
var num2 = document.getElementById('num2').value;
var result = document.getElementById('result');
var operation = document.getElementById('operation').value;
switch (operation) {
case '+':
result.value = parseFloat(num1) + parseFloat(num2);
break;
case '-':
result.value = parseFloat(num1) - parseFloat(num2);
break;
case '*':
result.value = parseFloat(num1) * parseFloat(num2);
break;
case '/':
result.value = parseFloat(num1) / parseFloat(num2);
break;
default:
result.value = '无效操作';
}
}
</script>
</head>
<body>
<input type="text" id="num1" placeholder="数字1" />
<select id="operation">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" id="num2" placeholder="数字2" />
<button onclick="calculate()">计算</button>
<input type="text" id="result" readonly />
</body>
</html>
禁止页面滚动
<!DOCTYPE html>
<html>
<head>
<title>禁止页面滚动</title>
<script>
window.onscroll = function() {
window.scrollTo(0, 0);
};
</script>
</head>
<body>
<h1>禁止页面滚动</h1>
<p>页面将不会滚动</p>
</body>
</html>
5. 常用框架与库
使用Vue.js实现数据绑定
<!DOCTYPE html>
<html>
<head>
<title>Vue.js数据绑定</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h1>{{ message }}</h1>
<input v-model="message" placeholder="输入内容" />
</div>
<script>
var app = new Vue({
el: '#app',
data: {
message: 'Hello, Vue.js!'
}
});
</script>
</body>
</html>
使用jQuery实现轮播图
<!DOCTYPE html>
<html>
<head>
<title>jQuery轮播图</title>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
<style>
.carousel {
width: 300px;
height: 200px;
overflow: hidden;
position: relative;
}
.carousel ul {
position: absolute;
left: 0;
width: 1500px;
}
.carousel li {
float: left;
width: 300px;
height: 200px;
}
</style>
</head>
<body>
<div class="carousel">
<ul>
<li style="background: red;"></li>
<li style="background: green;"></li>
<li style="background: blue;"></li>
</ul>
</div>
<script>
$(function() {
var $carousel = $('.carousel ul');
var $items = $('.carousel li');
var itemWidth = $items.eq(0).width();
var totalWidth = $items.length * itemWidth;
$carousel.width(totalWidth);
setInterval(function() {
$carousel.animate({
left: '-=' + itemWidth
}, 1000, function() {
$carousel.children(':first').appendTo($carousel);
$carousel.css('left', 0);
});
}, 2000);
});
</script>
</body>
</html>
以上是部分精选的Web前端代码案例,希望能帮助你快速入门和实践。在实际开发中,你可以根据需求灵活运用这些案例,提升自己的技能水平。
