doushi5024 2017-12-21 09:25
浏览 36
已采纳

如何将图像添加为自己的背景图像属性?

In Wordpress, I wanted to duplicate the images of the posts, so I can set one of them as background, and then blur it, creating a nice effect, without having to change all my html structure.

How can I do that, if possible, with PHP? I tried long time ago to achieve with JQuery, but at that time I didn't manage to make it:

$(".post-cover").each(function(){
  var img = $(this).find("img");
  $(this).css({
    "background-image": "url('"+img.prop("src")+"')",
    //Any other CSS background propriety
    //If your div don't have a fixed width and height
    width: img.width(),
    height: img.height()
  });
  img.remove();
});

If I used Jquery, where should I implement it?

My structure is

<div class="post-cover">
  <img src="#"/>
</div>

and the final result should be something like:

enter image description here

  • 写回答

2条回答 默认 最新

  • douyu0725 2017-12-21 10:11
    关注

    Could be like this:

    $( document ).ready(function() {
      $('.post-cover img').each(function() {
        $(this).before('<img src="'+ $(this).attr('src')+'" class="blur">');
      });
    });
    .post-cover {
        position: relative;
    }
    .post-cover img {
        width: 250px;
        height: auto;
        display: block;
        position: relative;
        padding: 50px;
    }
    
    .post-cover img.blur {
        content: '';
        display: block;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        -webkit-filter: blur(25px);
        filter: blur(25px);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="post-cover">
      <img src="https://placeimg.com/640/480/arch">
    </div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口