
在WordPress网站中使用Elementor构建页面时,我们常常会集成自定义的HTML/CSS代码来创建独特的产品卡片或其他UI组件。有时,我们希望卡片上的一个按钮不仅仅是跳转到新页面,而是能在当前页面内动态地显示一个外部服务或嵌入式内容,例如一个日程预约组件(如Calendly)、一个表单或一个视频播放器。这种需求可以通过JavaScript来控制元素的显示与隐藏来实现。
本教程将以一个产品卡片按钮点击后显示Calendly预约组件为例,详细讲解实现步骤。
要实现按钮点击后动态显示内容,主要依赖以下三个方面的协同工作:
首先,我们需要在Elementor的“自定义HTML”小部件中,或通过主题文件(如functions.php结合短代码)添加产品卡片的HTML结构。确保你的产品卡片中有一个可点击的按钮元素,通常是一个<a>标签。
<div class="card">
<div class="imgBox">
<img src="https://toppng.com/uploads/thumbnail/valorant-11608280122mrhwm6iwqx.png" alt="mouse corsair" class="mouse">
</div>
<div class="contentBox">
<h3>Mouse Corsair M65</h3>
<h2 class="price">61.<small>98</small> €</h2>
<!-- 这是我们的目标按钮,添加onclick事件 -->
<a href="#" class="buy" onclick="toggleVisibility('calendlyWidgetContainer')">BRONEERI</a>
</div>
</div>说明:
接下来,我们需要将Calendly的嵌入代码放置在产品卡片HTML的合适位置,并将其包裹在一个具有特定ID的div中。关键是,这个div需要通过内联样式或外部CSS设置为初始隐藏状态(display: none;)。
<div class="card">
<!-- ... 产品卡片其他内容 ... -->
<div class="contentBox">
<h3>Mouse Corsair M65</h3>
<h2 class="price">61.<small>98</small> €</h2>
<a href="#" class="buy" onclick="toggleVisibility('calendlyWidgetContainer')">BRONEERI</a>
</div>
<!-- Calendly组件容器,初始隐藏 -->
<div id="calendlyWidgetContainer" style="min-width:320px;height:630px;display:none;">
<!-- Calendly的iframe嵌入代码 -->
<iframe src="https://calendly.com/mokez/neace?hide_gdpr_banner=1" style="width:100%; height:100%; border:none;"></iframe>
</div>
<!-- Calendly官方的widget.js脚本,如果Calendly需要它来正确渲染iframe或提供其他功能,则保留 -->
<script type="text/javascript" src="https://assets.calendly.com/assets/external/widget.js" async></script>
</div>说明:
现在,我们需要编写一个JavaScript函数来处理按钮点击事件,并切换calendlyWidgetContainer的显示状态。这个脚本应该放在HTML代码的<script>标签中,通常位于页面的底部或与HTML内容紧密相关的区域。
<!-- ... 上面的HTML和CSS代码 ... -->
<script>
/**
* 切换指定元素的显示/隐藏状态。
* @param {string} elementId - 要切换显示状态的元素的ID。
*/
function toggleVisibility(elementId) {
var element = document.getElementById(elementId);
if (element) { // 确保元素存在
if (element.style.display === "none") {
element.style.display = "block"; // 将其显示为块级元素
// 如果需要,可以在这里滚动到该元素
// element.scrollIntoView({ behavior: 'smooth' });
} else {
element.style.display = "none"; // 将其隐藏
}
}
}
</script>说明:
将上述HTML、CSS和JavaScript代码集成到Elementor页面中:
将所有部分整合到一个Elementor的“自定义HTML”小部件中,看起来会像这样:
<div class="card">
<div class="imgBox">
<img src="https://toppng.com/uploads/thumbnail/valorant-11608280122mrhwm6iwqx.png" alt="mouse corsair" class="mouse">
</div>
<div class="contentBox">
<h3>Mouse Corsair M65</h3>
<h2 class="price">61.<small>98</small> €</h2>
<a href="#" class="buy" onclick="toggleVisibility('calendlyWidgetContainer')">BRONEERI</a>
</div>
<!-- Calendly组件容器,初始隐藏 -->
<div id="calendlyWidgetContainer" style="min-width:320px;height:630px;display:none;">
<iframe src="https://calendly.com/mokez/neace?hide_gdpr_banner=1" style="width:100%; height:100%; border:none;"></iframe>
</div>
<script type="text/javascript" src="https://assets.calendly.com/assets/external/widget.js" async></script>
</div>
<style>
/* 产品卡片的基础样式 */
* {
margin: 0;
padding: 0;
font-family: "Istok Web", sans-serif;
box-sizing: border-box; /* 添加box-sizing */
}
.card {
position: relative;
width: 320px;
height: 480px;
background: #0f1923;
border-radius: 20px;
overflow: hidden;
margin: 20px auto; /* 居中显示 */
}
.card::before {
content: "";
position: absolute;
top: -50%;
width: 100%;
height: 100%;
background: #ff4655;
transform: skewY(345deg);
transition: 0.5s;
}
.card:hover::before {
top: -70%;
transform: skewY(390deg);
}
.card::after {
content: "CORSAIR";
position: absolute;
bottom: 0;
left: 0;
font-weight: 600;
font-size: 6em;
color: rgba(0, 0, 0, 0.1);
}
.card .imgBox {
position: relative;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
padding-top: 20px;
z-index: 1;
}
.card .contentBox {
position: relative;
padding: 20px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
z-index: 2;
}
.card .contentBox h3 {
font-size: 18px;
color: white;
font-weight: 500;
text-transform: uppercase;
letter-spacing: 1px;
}
.card .contentBox .price {
font-size: 24px;
color: white;
font-weight: 700;
letter-spacing: 1px;
}
.card .contentBox .buy {
position: relative;
top: 100px; /* 初始隐藏在下方 */
opacity: 0;
padding: 10px 30px;
margin-top: 15px;
color: #FFFFFF;
text-decoration: none;
background: #ff4655;
border-radius: 30px;
text-transform: uppercase;
letter-spacing: 1px;
transition: 0.5s;
}
.card:hover .contentBox .buy {
top: 0; /* 鼠标悬停时显示 */
opacity: 1;
}
.mouse {
height: 300px;
width: auto;
}
</style>
<script>
function toggleVisibility(elementId) {
var element = document.getElementById(elementId);
if (element) {
if (element.style.display === "none") {
element.style.display = "block";
// 可选:显示后滚动到该元素,提升用户体验
// element.scrollIntoView({ behavior: 'smooth', block: 'start' });
} else {
element.style.display = "none";
}
}
}
</script>通过以上步骤,你就可以在WordPress Elementor中为产品卡片按钮添加动态显示外部内容的功能,从而提升用户交互性和页面的功能性。
以上就是WordPress Elementor产品卡片按钮集成外部链接:实现内容动态显示的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号