使用 html+css+js 写一个pv uv统计页面,要求页面布局优美,直接给出完整的案例·1
时间: 2023-09-19 09:05:53 浏览: 55
### 回答1:
以下是使用 HTML、CSS、JavaScript 构建 PV/UV 统计页面的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>PV/UV 统计</title>
<style>
/* CSS 样式 */
#pv-uv-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.stat {
width: 300px;
height: 200px;
border: 1px solid #ccc;
padding: 20px;
margin: 0 20px;
text-align: center;
}
.stat h2 {
margin: 0;
}
</style>
</head>
<body>
<div id="pv-uv-container">
<div class="stat">
<h2>PV</h2>
<p id="pv-count"></p>
</div>
<div class="stat">
<h2>UV</h2>
<p id="uv-count"></p>
</div>
</div>
<script>
// JavaScript 代码
var pvCount = 0;
var uvCount = new Set();
var currentUvCount = 0;
// 统计 PV
window.onload = function() {
pvCount++;
document.getElementById("pv-count").innerHTML = pvCount;
}
// 统计 UV
window.onbeforeunload = function() {
var uv = Math.random().toString(36).substr(2);
if (!uvCount.has(uv)) {
uvCount.add(uv);
currentUvCount = uvCount.size;
document.getElementById("uv-count").innerHTML = currentUvCount;
}
}
</script>
</body>
</html>
```
这个示例使用了 HTML、CSS 和 JavaScript 来构建页面,使用了 flex 布局来美化页面布局。页面包括两个统计 PV 和 UV 的区块,分别显示在页面中央。在 JavaScript 中,使用了 window.onload 事件来统计 PV,并在 window.onbeforeunload 事件中使用了随机数来统计 UV。
PV:页面被访问的次数
UV
### 回答2:
以下是一个使用HTML、CSS和JavaScript编写的基本的PV(页面访问量)和UV(唯一访客数)统计页面的示例。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PV/UV 统计</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f2f2f2;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
.stats-container {
display: flex;
justify-content: center;
}
.stat {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: white;
width: 150px;
height: 100px;
margin: 10px;
padding: 10px;
border-radius: 5px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}
.stat-value {
font-size: 24px;
font-weight: bold;
}
.stat-label {
margin-top: 5px;
font-size: 14px;
}
</style>
</head>
<body>
<h1>PV/UV 统计</h1>
<div class="stats-container">
<div class="stat">
<span class="stat-value" id="pvCount">0</span>
<span class="stat-label">PV</span>
</div>
<div class="stat">
<span class="stat-value" id="uvCount">0</span>
<span class="stat-label">UV</span>
</div>
</div>
<script>
// 模拟PV和UV数据
var pv = 0;
var uv = 0;
// 更新界面上的PV和UV统计数值
function updateStats() {
document.getElementById('pvCount').textContent = pv;
document.getElementById('uvCount').textContent = uv;
}
// 模拟每个页面访问时的统计操作
function trackPageView() {
pv++;
updateStats();
}
// 模拟每个唯一访客访问时的统计操作
function trackUniqueVisitor() {
uv++;
updateStats();
}
// 模拟页面加载完成后的初始化操作
window.onload = function() {
trackPageView(); // 每次页面加载都算做一次PV
trackUniqueVisitor(); // 每个唯一访客只算一次UV
};
</script>
</body>
</html>
```
这个示例代码中,使用了HTML创建了一个包含标题、PV和UV统计的页面。CSS用于美化页面布局,JavaScript用于模拟页面访问和统计操作。每当页面加载完成(包括刷新)时,PV和UV计数会自动增加,并通过JavaScript更新到页面上。
### 回答3:
以下是一个使用HTML、CSS和JS编写的PV UV统计页面的完整案例:
```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PV UV统计页面</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
margin-top: 50px;
font-size: 36px;
color: #333;
}
.counter {
margin-top: 50px;
font-size: 24px;
color: #666;
}
</style>
</head>
<body>
<h1>PV UV统计页面</h1>
<div class="counter">
<p>PV: <span id="pvCount">0</span></p>
<p>UV: <span id="uvCount">0</span></p>
</div>
<script>
// 假设以下为统计逻辑
let pvCount = 0;
let uvCount = 0;
function incrementPV() {
pvCount++;
document.getElementById("pvCount").textContent = pvCount;
}
function incrementUV() {
uvCount++;
document.getElementById("uvCount").textContent = uvCount;
}
// 模拟页面加载时的PV和UV增加
window.addEventListener("load", function() {
incrementPV();
incrementUV();
});
</script>
</body>
</html>
```
这个案例是一个简单的PV UV统计页面,包括一个页面标题、PV和UV计数器。在加载页面时,PV和UV计数都会增加一次,并在页面中显示出来。PV和UV的计数逻辑是模拟的,实际应用中需要根据具体的需求和技术实现来完成。页面使用了基本的HTML结构和CSS样式进行布局,使页面看起来优美。JS代码用于实现计数功能和初始化计数值。