Javascript 模拟背景大小:封面在<;视频>;或<;img>;

Javascript 模拟背景大小:封面在<;视频>;或<;img>;,javascript,jquery,css,Javascript,Jquery,Css,如何在像或这样的html元素上模拟背景大小:cover的功能 我希望它能像这样工作 background-size: cover; background-position: center center; 在我们长长的评论部分之后,我想这就是您想要的,它基于jQuery: HTML: <img width="100%" id="img" src="http://uploads8.wikipaintings.org/images/william-adolphe-bouguereau/self

如何在像
这样的html元素上模拟
背景大小:cover
的功能

我希望它能像这样工作

background-size: cover;
background-position: center center;

在我们长长的评论部分之后,我想这就是您想要的,它基于jQuery:

HTML:

<img width="100%" id="img" src="http://uploads8.wikipaintings.org/images/william-adolphe-bouguereau/self-portrait-presented-to-m-sage-1886.jpg">
<script type="text/javascript">
window.onload = function(){
       var img = document.getElementById('img')
       if(img.clientHeight<$(window).height()){
            img.style.height=$(window).height()+"px";
       }
       if(img.clientWidth<$(window).width()){
            img.style.width=$(window).width()+"px";
       } 
}
​</script>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
body{
    overflow: hidden;
}
<div style="width:100px; max-height: 100px;" id="div">
     <img width="100%" id="img" src="http://uploads8.wikipaintings.org/images/william-adolphe-bouguereau/self-portrait-presented-to-m-sage-1886.jpg">
</div>
<script type="text/javascript">
window.onload = function(){
       var img = document.getElementById('img')
       if(img.clientHeight<$('#div').height()){
            img.style.height=$('#div').height()+"px";
       }
       if(img.clientWidth<$('#div').width()){
            img.style.width=$('#div').width()+"px";
       } 
}
​</script>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
div{
   overflow: hidden;
}
/*
filename:style.css
*/
body {
    margin:0;
}

#vid video{
position: absolute; 
right: 0; 
top: 0;
min-width: 100%; 
min-height: 100%;
width: auto; 
height: auto; 
}

#contain {
width:100%;
height:100%;
zoom:1%;/*Without this the video will be stretched and skewed*/ 
}
​上面的代码使用浏览器的宽度和高度如果在div中执行此操作,则必须将其更改为以下内容:

对于Div:

<img width="100%" id="img" src="http://uploads8.wikipaintings.org/images/william-adolphe-bouguereau/self-portrait-presented-to-m-sage-1886.jpg">
<script type="text/javascript">
window.onload = function(){
       var img = document.getElementById('img')
       if(img.clientHeight<$(window).height()){
            img.style.height=$(window).height()+"px";
       }
       if(img.clientWidth<$(window).width()){
            img.style.width=$(window).width()+"px";
       } 
}
​</script>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
body{
    overflow: hidden;
}
<div style="width:100px; max-height: 100px;" id="div">
     <img width="100%" id="img" src="http://uploads8.wikipaintings.org/images/william-adolphe-bouguereau/self-portrait-presented-to-m-sage-1886.jpg">
</div>
<script type="text/javascript">
window.onload = function(){
       var img = document.getElementById('img')
       if(img.clientHeight<$('#div').height()){
            img.style.height=$('#div').height()+"px";
       }
       if(img.clientWidth<$('#div').width()){
            img.style.width=$('#div').width()+"px";
       } 
}
​</script>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
div{
   overflow: hidden;
}
/*
filename:style.css
*/
body {
    margin:0;
}

#vid video{
position: absolute; 
right: 0; 
top: 0;
min-width: 100%; 
min-height: 100%;
width: auto; 
height: auto; 
}

#contain {
width:100%;
height:100%;
zoom:1%;/*Without this the video will be stretched and skewed*/ 
}
HTML:

<img width="100%" id="img" src="http://uploads8.wikipaintings.org/images/william-adolphe-bouguereau/self-portrait-presented-to-m-sage-1886.jpg">
<script type="text/javascript">
window.onload = function(){
       var img = document.getElementById('img')
       if(img.clientHeight<$(window).height()){
            img.style.height=$(window).height()+"px";
       }
       if(img.clientWidth<$(window).width()){
            img.style.width=$(window).width()+"px";
       } 
}
​</script>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
body{
    overflow: hidden;
}
<div style="width:100px; max-height: 100px;" id="div">
     <img width="100%" id="img" src="http://uploads8.wikipaintings.org/images/william-adolphe-bouguereau/self-portrait-presented-to-m-sage-1886.jpg">
</div>
<script type="text/javascript">
window.onload = function(){
       var img = document.getElementById('img')
       if(img.clientHeight<$('#div').height()){
            img.style.height=$('#div').height()+"px";
       }
       if(img.clientWidth<$('#div').width()){
            img.style.width=$('#div').width()+"px";
       } 
}
​</script>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
div{
   overflow: hidden;
}
/*
filename:style.css
*/
body {
    margin:0;
}

#vid video{
position: absolute; 
right: 0; 
top: 0;
min-width: 100%; 
min-height: 100%;
width: auto; 
height: auto; 
}

#contain {
width:100%;
height:100%;
zoom:1%;/*Without this the video will be stretched and skewed*/ 
}
我还应该说,我只测试过这是谷歌浏览器。。。下面是一个JSFIDLE:
我是这样做的。一个有效的例子是

var min_w=300;//允许的最小视频宽度
var vid_w_orig;//原始视频尺寸
var vid_h_orig;
jQuery(function(){//在加载DOM后运行
vid_w_orig=parseInt(jQuery('video').attr('width'));
vid_h_orig=parseInt(jQuery('video').attr('height'));
$(“#调试”).append(DOM-loaded

”; 调整大小(函数(){resizeToCover();}); jQuery(window.trigger('resize'); }); 函数resizeToCover(){ //将视频视口设置为窗口大小 jQuery(“#视频视口”).width(jQuery(window.width()); jQuery(“#视频视口”).height(jQuery(window.height()); //使用水平/垂直的最大比例因子 var scale_h=jQuery(window).width()/vid_w_orig; var scale\u v=jQuery(window).height()/vid\u h\u orig; var标度=标度h>标度v?标度h:标度v; //不允许缩放宽度<最小视频宽度 如果(scale*vid_w_orig”); jQuery(“#调试”).append(win\uh:“+jQuery(window.height()+”

”); jQuery(“#调试”).append(“viewport_w:”+jQuery(“#视频viewport”).width()+”

”; jQuery(“#调试”).append(“viewport_h:”+jQuery(“#视频viewport”).height()+”

”; jQuery(“#调试”).append(video w:“+jQuery('video').width()+”

”); jQuery(“#调试”).append(video_h:“+jQuery('video').height()+”

”); jQuery(“#调试”)。追加(vid_w_orig:“+vid_w_orig+”

”; jQuery(“#调试”).append(vid_h_orig:“+vid_h_orig+”

”; jQuery(“#调试”)。追加(scale:“+scale+”

”); };
#视频视口{
位置:绝对位置;
排名:0;
溢出:隐藏;
z索引:-1;/*用于通过单击访问视频*/
}
#调试{
位置:绝对位置;
排名:0;
z指数:100;
颜色:#fff;
字号:12号;
}

我也将发布此解决方案,因为我遇到了此问题,但其他解决方案不适合我的情况

我认为适当模拟
背景尺寸:封面css属性而不是元素背景图像属性,您必须将图像纵横比与当前窗口纵横比进行比较,因此无论大小(以及图像高于更宽的情况),窗口都是填充窗口的元素(而且还要居中,尽管我不知道这是否是一项要求)

为了简单起见,使用图像,我相信视频元素也可以

首先获取元素纵横比(加载后),然后附加窗口大小调整处理程序,触发一次以进行初始大小调整:

var img = document.getElementById( "background-picture" ),
    imgAspectRatio;

img.onload = function() {
    // get images aspect ratio
    imgAspectRatio = this.height / this.width;
    // attach resize event and fire it once
    window.onresize = resizeBackground;
    window.onresize();
}
然后,在调整大小处理程序中,您应该首先通过比较窗口的当前纵横比和图像的原始纵横比来确定是填充宽度还是填充高度

function resizeBackground( evt ) {

// get window size and aspect ratio
var windowWidth = window.innerWidth,
    windowHeight = window.innerHeight;
    windowAspectRatio = windowHeight / windowWidth;

//compare window ratio to image ratio so you know which way the image should fill
if ( windowAspectRatio < imgAspectRatio ) {
    // we are fill width
    img.style.width = windowWidth + "px";
    // and applying the correct aspect to the height now
    img.style.height = (windowWidth * imgAspectRatio) + "px";
    // this can be margin if your element is not positioned relatively, absolutely or fixed
    // make sure image is always centered
    img.style.left = "0px";
    img.style.top = (windowHeight - (windowWidth * imgAspectRatio)) / 2 + "px";
} else { // same thing as above but filling height instead
    img.style.height = windowHeight + "px";
    img.style.width = (windowHeight / imgAspectRatio) + "px";
    img.style.left = (windowWidth - (windowHeight / imgAspectRatio)) / 2 + "px";
    img.style.top = "0px";
}
功能调整背景(evt){
//获取窗口大小和纵横比
var windowWidth=window.innerWidth,
windowHeight=window.innerHeight;
windowAspectRatio=窗高/窗宽;
//比较窗口比率和图像比率,以便知道图像应以何种方式填充
if(windowAspectRatio
}

对图像使用背景覆盖是可以的,宽度100%也是可以的。对于
,这些都不是最佳选择,而且这些答案过于复杂。您不需要jQuery或JavaScript来完成全宽视频背景

请记住,我的代码不会像cover will那样用视频完全覆盖背景,相反,它会使视频尽可能大,以保持纵横比,并仍然覆盖整个背景。任何多余的视频都会从页面边缘流出,这取决于视频的锚定位置

答案很简单

只需使用HTML5视频代码,或者类似的东西:(整页测试)

html,正文{
宽度:100%;
身高:100%;
溢出:隐藏;
}
#视频{
位置:绝对位置;
最高:50%;
左:50%;
-webkit转换:translateX(-50%)translateY(-50%);
转化:translateX(-50%)translateY(-50%);
最小宽度:100%;
.container
  .detail-img.d-flex.align-items-center
    %img{src: 'http://placehold.it/1000x700'}
.detail-img { // simulate background: center/cover
  max-height: 400px;
  overflow: hidden;

  img {
    width: 100%;
  }
}
#video-container {
    overflow: hidden;
}

#video{
    width: 100%;
    position:absolute; 
    top: 0; 
    right: 0; 
    z-index: -1; 
}