使用纯HTML和CSS(最简单,无交互)
这种方法最直接,二维码图片会一直固定在屏幕右侧。

(图片来源网络,侵删)
实现步骤:
- 准备图片:将你的微信二维码保存为
.png或.jpg格式,建议尺寸在 200x200 像素左右,确保清晰。 - HTML 结构:在
<body>标签内,创建一个<div>作为二维码的容器。 - CSS 样式:使用 CSS 的
position: fixed属性将容器固定在屏幕右侧,并添加一些间距和美化样式。
完整代码示例:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">网页右侧二维码示例</title>
<style>
/* 基本页面样式,为了演示 */
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
color: #333;
}
.content {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h1 {
text-align: center;
}
p {
text-align: justify;
}
/* 二维码容器样式 */
.qr-code-container {
position: fixed; /* 固定定位 */
right: 20px; /* 距离右边 20px */
top: 50%; /* 垂直居中 */
transform: translateY(-50%); /* 向上自身高度的50%,实现完美垂直居中 */
z-index: 1000; /* 确保它在其他内容之上 */
text-align: center;
}
.qr-code-image {
width: 150px; /* 设置二维码宽度 */
height: 150px; /* 设置二维码高度 */
border-radius: 10px; /* 圆角 */
box-shadow: 0 4px 8px rgba(0,0,0,0.2); /* 阴影效果 */
}
.qr-code-text {
margin-top: 10px;
font-size: 14px;
color: #555;
background-color: rgba(255, 255, 255, 0.9);
padding: 5px 10px;
border-radius: 15px;
}
</style>
</head>
<body>
<div class="content">
<h1>欢迎访问我们的网站</h1>
<p>
这里是一些网页的正文内容,当你滚动页面时,你会发现右侧的二维码会一直固定在那里,不会随着页面的滚动而消失,这非常方便用户随时扫描关注我们的公众号。
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
<!-- 更多内容... -->
</div>
<!-- 二维码容器 -->
<div class="qr-code-container">
<img src="https://via.placeholder.com/150x150.png?text=WeChat+QR" alt="微信公众号二维码" class="qr-code-image">
<div class="qr-code-text">扫码关注公众号</div>
</div>
</body>
</html>
代码解释:
position: fixed;:这是关键,它使元素脱离正常的文档流,相对于浏览器窗口进行定位。right: 20px;:容器距离浏览器窗口右侧的距离。top: 50%;:容器顶部距离浏览器窗口顶部的50%。transform: translateY(-50%);:将容器自身向上移动其高度的一半,从而实现垂直居中。z-index: 1000;:设置一个较高的层级,防止被页面其他元素遮挡。
添加悬停效果(更友好)
在方案一的基础上,我们可以添加鼠标悬停(hover)效果,比如让二维码放大或显示提示信息,提升用户体验。
实现步骤:
在方案一的 CSS 中,为 .qr-code-container 添加 hover 伪类样式。
代码示例:
在 <style> 标签内添加以下 CSS:

(图片来源网络,侵删)
/* 为二维码容器添加悬停效果 */
.qr-code-container:hover .qr-code-image {
transform: scale(1.1); /* 放大到1.1倍 */
box-shadow: 0 8px 16px rgba(0,0,0,0.3); /* 增强阴影 */
transition: transform 0.3s ease, box-shadow 0.3s ease; /* 添加平滑过渡效果 */
}
/* 可选:为文字也添加悬停效果 */
.qr-code-container:hover .qr-code-text {
background-color: #07C160; /* 微信绿色 */
color: white;
}
效果:当用户将鼠标移到二维码上时,它会平滑地放大并显示更明显的阴影,文字背景色也会变成微信绿,交互感更强。
添加点击弹出层(更优雅)
为了不遮挡页面内容,我们可以让二维码默认以一个小的图标形式显示在右侧,当用户点击时,才弹出一个模态框(Modal)来展示大图和说明。
实现步骤:
- HTML:创建一个小的触发图标和一个隐藏的模态框。
- CSS:设置触发图标的样式,以及模态框的默认隐藏状态和弹出后的样式。
- JavaScript:监听触发图标的点击事件,控制模态框的显示和隐藏。
完整代码示例:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">网页右侧弹出二维码示例</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
color: #333;
}
.content {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h1 {
text-align: center;
}
/* 触发按钮样式 */
.qr-trigger {
position: fixed;
right: 20px;
top: 50%;
transform: translateY(-50%);
width: 60px;
height: 60px;
background-color: #07C160; /* 微信绿 */
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
box-shadow: 0 2px 8px rgba(0,0,0,0.2);
z-index: 1001;
transition: transform 0.3s ease;
}
.qr-trigger:hover {
transform: translateY(-50%) scale(1.1);
}
.qr-trigger img {
width: 40px;
height: 40px;
}
/* 模态框样式 */
.modal {
display: none; /* 默认隐藏 */
position: fixed;
z-index: 2000; /* 比触发按钮更高 */
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.5); /* 半透明黑色背景 */
animation: fadeIn 0.3s ease;
}
.modal-content {
background-color: #fefefe;
margin: 15% auto; /* 距离顶部15%,水平居中 */
padding: 20px;
border-radius: 10px;
width: 300px;
text-align: center;
box-shadow: 0 4px 20px rgba(0,0,0,0.3);
animation: slideIn 0.3s ease;
}
.modal-image {
width: 200px;
height: 200px;
margin-bottom: 15px;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
cursor: pointer;
line-height: 1;
}
.close:hover {
color: #000;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes slideIn {
from { transform: translateY(-50px); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
}
</style>
</head>
<body>
<div class="content">
<h1>欢迎访问我们的网站</h1>
<p>
这里是一些网页的正文内容,我们采用了更优雅的交互方式:点击右侧的微信图标,会弹出一个精美的二维码窗口,不会影响您浏览页面。
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
<!-- 更多内容... -->
</div>
<!-- 触发按钮 -->
<div class="qr-trigger" id="qrTrigger">
<img src="https://www.svgrepo.com/show/452256/wechat.svg" alt="微信图标">
</div>
<!-- 模态框 -->
<div id="qrModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<img src="https://via.placeholder.com/200x200.png?text=WeChat+QR" alt="微信公众号二维码" class="modal-image">
<p>扫码关注公众号</p>
</div>
</div>
<script>
// 获取元素
const modal = document.getElementById("qrModal");
const trigger = document.getElementById("qrTrigger");
const closeBtn = document.getElementsByClassName("close")[0];
// 点击触发按钮,打开模态框
trigger.onclick = function() {
modal.style.display = "block";
}
// 点击关闭按钮,关闭模态框
closeBtn.onclick = function() {
modal.style.display = "none";
}
// 点击模态框的背景部分,也可以关闭模态框
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>
代码解释:
- HTML:
qr-trigger是小的圆形触发按钮,qr-modal是隐藏的模态框。 - CSS:
.modal默认display: none,当需要显示时,通过 JavaScript 将其改为block。animation属性让弹出和关闭有平滑的过渡效果。 - JavaScript:通过
onclick事件监听器来控制模态框的显示和隐藏,逻辑清晰。
总结与选择建议
| 方案 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
| 纯HTML/CSS | 实现最简单,无需JS,加载快。 | 一直占用页面空间,可能影响某些屏幕尺寸下的阅读体验。 | 博客文章、内容展示类页面,二维码信息是核心。 |
| 添加悬停效果 | 提升了交互性,用户友好。 | 仍然会一直占用空间。 | 对UI细节有要求,希望有轻微反馈的网站。 |
| 点击弹出层 | 最优雅,不遮挡页面内容,用户体验最佳。 | 代码量稍多,需要一点JS知识。 | 大多数商业网站、产品官网、SaaS平台,追求专业和美观。 |
建议:对于大多数现代网站,方案三(点击弹出层) 是最佳选择,因为它在功能和美观之间取得了最好的平衡,如果您的网站非常简单,或者二维码本身就是页面的重要组成部分,方案一 也是一个不错的选择。

(图片来源网络,侵删)
