如何在CSS上向Image添加播放图标

时间:2015-06-25 09:54:00

标签: javascript html css

我正在寻找如何在CSS上为图片添加播放图标。

我尝试了这段代码,但看起来不对,我希望它出现在中心:

    .video { position: relative; }

.video a {
   position: absolute;
   display: block;
   background: url(http://www.slatecube.com/images/play-btn.png);
   height: 100%;
   width: 100%;
   top: 20px;
   left: 20px;
}

html:

<div class="video" >  
<img  style="height:200px"src="http://san.capitalafrique.com/imatin.net/articles/images/lionel-messi.jpg"  />
    <a href="#"></a>
</div>

这里是jsFiddle:

DEMO

6 个答案:

答案 0 :(得分:3)

DEMO

you should set background-size

.video a {
  position: absolute;
  display: block;
  background: url(http://www.slatecube.com/images/play-btn.png);
  height: 100%;
  width: 100%;
  top: 75px;
  left: 150px;
  background-size: 50px 50px;
  background-repeat: no-repeat;
}

答案 1 :(得分:0)

Use background-size:contain; and center the background:

background: url(http://www.slatecube.com/images/play-btn.png) no-repeat center;
background-size:contain;

JsFiddle: http://jsfiddle.net/z5y8ht4y/2/

答案 2 :(得分:0)

  

点击甚至使用jquery或javascript

Jquery

示例: http://jsfiddle.net/kevalbhatt18/z5y8ht4y/4/

如果不缩小宽度,请将播放按钮的宽度缩小为 11%,然后在左侧区域中单击

以100%宽度播放http://jsfiddle.net/kevalbhatt18/z5y8ht4y/3

查看此示例

$('img').click(function(){
alert('clicked')

})

如果你想要100%的宽度,或者使用z-index,但它可能会破坏你的布局,因为游戏有位置:绝对

<强>的Javascript

<强> HTML  的

<img onclick="clickfunction()" style="height:200px" src="http://san.capitalafrique.com/imatin.net/articles/images/lionel-messi.jpg" /> <a href="#"></a>

<强>脚本

clickfunction = function () {
   alert()
}

答案 3 :(得分:0)

仅添加此CSS

.video a {
    position: absolute;
    display: block;
    background: url(http: //www.slatecube.com/images/play-btn.png);
    height: 50 px;
    width: 100 %;
    top: 92 px;
    background - size: 50 px 50 px;
    background - repeat: no - repeat;
    background - position: center;
}

.video {
    position: relative;
    float: left;
}

答案 4 :(得分:0)

可能是这个吗? (如果您知道图像的大小)

&#13;
&#13;
.video { 
    position: relative; 
    background-image: url('http://san.capitalafrique.com/imatin.net/articles/images/lionel-messi.jpg');
    width:400px;
    height:200px;
}
.link{
    width:100%; 
    height:100%; 
    position:absolute; 
    left:0px; 
    top:0px; 
    background-color:rgba(255,255,255,0.5);
}
.link:hover{
    background-color:rgba(255,255,255,0.7);
}
.playBtn {
   position: absolute;
   background-image:url("http://www.slatecube.com/images/play-btn.png");
   background-size: 150px 150px;
   height: 150px;
   width: 150px;
   top: 50%;
   left: 50%;
   transform: translate(-50%, -50%);
    z-index:999;
}
&#13;
<div class="video" >  
    <a href="#" class="link">
        <div class="playBtn"></div>
    </a>
</div>
    
&#13;
&#13;
&#13;

答案 5 :(得分:0)

您可以在没有图片的情况下实现此目的,这可以减少加载时间:http://jsfiddle.net/z5y8ht4y/5/

* {
  box-sizing: border-box;
}
.video {
  position: relative;
  display: table;
  height: 200px;
  width: 357px;
  background: url(http://san.capitalafrique.com/imatin.net/articles/images/lionel-messi.jpg) no-repeat;
  background-size: 100%;
}
.video a {
  display: table-cell;
  height: 100%;
  width: 100%;
  text-align: center;
  vertical-align: middle;
}
.video a .circle {
  display: inline-block;
  border: 3px solid white;
  border-radius: 25px;
  padding: 20px;
  height: 10px;
  width: 10px;
}
.video a .circle .triangle {
  display: inline-block;
  width: 0;
  height: 0;
  border-top: 10px solid transparent;
  border-bottom: 10px solid transparent;
  border-left: 10px solid white;
  transform: translate(-25%, -50%);
}
<div class="video">
  <a href="#">
    <span class="circle">
      <span class="triangle"></span>
    </span>
  </a>
</div>