You need to enable JavaScript to run this app. 如何在flexslider画廊中当$pricesold为upcoming时展示叠加图片
优惠活动
大模型
产品
解决方案
定价
更多

如何在flexslider画廊中当$pricesold为upcoming时展示叠加图片

问题根因

你遇到的叠加层无法正常展示核心是两个布局逻辑问题:

  • 绝对定位元素依赖最近的设置了非static定位的父容器计算位置,你现有代码的#slider容器没有设置position: relative,叠加层的绝对定位会以页面根节点为基准,位置完全偏离画廊范围
  • 叠加层没有放在#slider容器内部,和画廊元素的层级关系不匹配,z-index设置无法生效

修改方案

直接替换你现有画廊代码即可,已整合叠加层逻辑:

<!-- 给slider增加relative定位,作为叠加层的定位父容器 -->
<div id="slider" class="flexslider" style="position: relative;">
    <?php 
    // 条件满足时输出叠加层,放在slider内部和ul同级,固定在最上层不随轮播滚动
    if ($pricesold == 'upcoming') { ?>
        <div id="ManagerSpecial" style="z-index: 50; position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none;">
            <img alt="特惠活动" src="https://test.co.uk/testpng.png" style="max-width: 100%; max-height: 100%;" />
        </div>
    <?php } ?>
    <ul class="slides cardeal_slide">
        <?php
        if ($autoshowroom_images = get_post_meta(get_the_ID(), 'images')) {
            foreach ($autoshowroom_images[0] as $image) { ?>
                <li data-src="<?php echo esc_url(wp_get_attachment_url($image)); ?>">
                    <a><img src="<?php echo esc_url(wp_get_attachment_url($image)); ?>"
                            alt="<?php echo esc_attr(get_the_title()); ?>"/></a>
                </li>
            <?php }
        }
        ?>
    </ul>
</div>

补充说明

新增的样式是为了适配大多数场景,你可以根据需求调整:

  • 给叠加层加width: 100%; height: 100%是为了撑满整个画廊容器,你可以根据PNG尺寸自定义宽高和位置
  • pointer-events: none是为了避免叠加层挡住下层画廊的点击、滑动操作,不影响轮播正常使用
  • 图片加max-width: 100%; max-height: 100%;是为了避免PNG尺寸过大溢出画廊范围

内容的提问来源于stack exchange,提问作者PaulTDS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 07:15:03