如何在包含JavaScript的div上放置图片?

时间:2018-07-20 00:53:54

标签: javascript html css

我已经开始使用Codepen娱乐了,我想尝试一下这个名为“ Croppie”(我是新手,还在学习JavaScript)的JavaScript代码。

我想在包含JavaScript的div上方放置图片。但是图像没有显示,而是隐藏在div后面。我也希望能够在viewport中移动图像,即使图像在其顶部。

我尝试弄乱z-index,但是我想要放在顶部的图像一直隐藏在div的后面。 Here is the CodePen.(在我的问题中不加任何代码就不会让我发帖,所以这就是我在CSS中谈论的内容。)

.mask {
  max-width: 100%;
  max-height: 100%;
  background-image: url('http://eprogramers.com/triangle.gif');
  position: absolute;
  z-index: 1;
}

#demo-basic {
  width: 300px;
  height: 300px;
  margin: auto;
  z-index: -1;
}

1 个答案:

答案 0 :(得分:2)

我更改了mask CSS类的样式,如下所示:

.mask{
    width: 100%;
    height: 100%;
    background-image:url('http://eprogramers.com/triangle.gif');
    background-repeat:no-repeat;
    position:absolute;
    z-index:99;
}

请检查以下代码段:

//create a variable called basic, the variable attaches to div called demo-basic, which is an instance of croppie
var basic = $('#demo-basic').croppie({
  //awknowledge the viewport
    viewport: {
        width: 150,
        height: 200
    }
});

//basic variable.croppie binds with url of cat
basic.croppie('bind', {
    url: 'https://foliotek.github.io/Croppie/demo/cat.jpg',
    points: [0,0,0,0]
});
body {
    background-color: pink;  
}

.mask{
    width: 100%;
    height: 100%;
    background-image:url('http://eprogramers.com/triangle.gif');
    background-repeat:no-repeat;
    position:absolute;
    z-index:99;
}

#demo-basic {
    width: 300px;
    height: 300px;
    margin: auto;
    z-index: -1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js?ver=3.4.2"></script>

<script src="https://foliotek.github.io/Croppie/croppie.js"></script>

<link rel="Stylesheet" type="text/css" href="https://foliotek.github.io/Croppie/croppie.css" />

<div id='demo-basic'>
  <div class="mask"></div> 
</div>