1 回答

TA贡献1840条经验 获得超5个赞
我以前做过这个,但以不同的方式参见下面的代码:
1)我已经使用了 Advance Custom field Repeater plugin pluginlike我已经安装了它并使用它创建自定义字段,请参见此处(https://prnt.sc/q6yohq)
2)从管理员的页面中插入值,例如我想要这个值“stackoverflowpage”页面(我已经制作了模板)所以从管理员我将插入值看到这里(https://prnt.sc/q6yqak)
3)在模板文件( 。
<?php
if( have_rows('allfields') ): ?>
<?php
$i=1;
while( have_rows('allfields') ): the_row();
// vars
$image = get_sub_field('image');
$title = get_sub_field('title');
$content = get_sub_field('description');
$link = get_sub_field('link');
?>
<table style="border-collapse: collapse; width: 100%">
<tbody>
<tr>
<td >
<div id="slides_<?php echo $i ?>" class="divcommonclass">
<table style="border-collapse: collapse; width: 100%">
<tbody>
<tr>
<td style="width: 100%" class="home-featured-heading">
<h4>
<a href="<?php echo $link ?>" target="_blank"><?php echo $title; ?> </a>
</h4>
</td>
</tr>
<tr>
<td style="width: 100%" class="home-featured-heading">
<?php echo $content; ?>
</td>
</tr>
</tbody>
</table>
</div>
<input type="hidden" id="bg-image" value="<?php echo $image['url']; ?>">
</td>
</tr>
</tbody>
</table>
<?php $i++;
endwhile; ?>
<?php endif; ?>
<div class="imageshow"></div>
4) 为我们要显示图像的 div 赋予样式
<style>
.imageshow {
background-repeat: no-repeat;
background-image: url('http://localhost/stackoverflow/wp-content/uploads/2019/12/person_2.jpg');
width: 100%;
height: 100%;
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
-ms-transition: all .5s ease;
-o-transition: all .5s ease;
transition: all .5s ease;
}
</style>
5)添加此脚本以在悬停在块上时显示图像
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.min.js">
</script>
<script>
$(document).ready(function(){
$('.divcommonclass')
.on('mouseover', function() {
var id = $(this).attr('id');
var imagpath = $('#'+id+ "+ #bg-image").val();
$('.imageshow').css('background-image', "url(" + imagpath + ")");
});
});
</script>
我没有使用 javascript 来获取 acf javascript 的图像值并在 div 中显示,因为我刚刚使用了 inut 类型 hidden 并在那里添加值,而在 javascript 中我只是通过其 id 获取该图像值
输出:请下载并查看视频(https://drive.google.com/file/d/1xTmSBlxn9NW0Klc4l-uOdA_l---oHuV7/view)
添加回答
举报