通过JavaScript调整视频大小

时间:2018-07-10 16:48:29

标签: javascript wordpress internet-explorer

我在www.remooble.com上有一个视频,其中的视频有时可以正确调整大小。这是何时何地(大部分时间)没有。

  1. 在Internet Explorer或Safari中(JavaScript并没有实现应有的作用)
  2. 用户第一次会话(在缓存内容之前)
  3. 在硬刷新上

这是我们正在运行的JavaScript。如前所述,它甚至无法在IE上运行(在CSS编辑器中无法调整大小)。

感谢您的帮助!让我知道我是否有任何遗漏。

;(function($, window, document, undefined) {
var $win = $(window);
var $doc = $(document);
var homeVideoIframe = $('#home-video');

function sliderInit( $slider, params ) {
    if ( $slider.length ) {
        $slider.lightSlider( params );
    }
}

function initQuotesSlider() {
    let height = 0;

    $('#homepageSlider > li').each((e, i) => {
        height = $(i).height();
    });
}

function adjustHomeVideoSize() {
    var width = homeVideoIframe.width();
    var height = width / 1.78;
    homeVideoIframe.height(height);
};

$doc.on( 'ready', function () {
    adjustHomeVideoSize();
});

$win.on('resize', function () {
    adjustHomeVideoSize();
});


$win.on( 'scroll, resize', function() {
    $( 'body' ).addClass( 'loaded' );
    initQuotesSlider();

    var testimonialSliderParam = {
        addClass: "quotes-slider",
        controls: false,
        slideMargin: 70,
        adaptiveHeight: true,
        vertical: false,
        autoWidth: true,
        pager: false,
        item: 4,
        onSliderLoad: function() {
            $('#homepageSlider').removeClass('cS-hidden');
        }
    };

    var $homepageTetimonialsSlider = $('#homepageSlider');

    if ( $win.width() > 1200 ) {
        testimonialSliderParam.items = 1;
    }

    sliderInit( $homepageTetimonialsSlider, testimonialSliderParam );
});

$win.on( 'load', function() {
    $( 'body' ).addClass( 'loaded' );

    initQuotesSlider();

    var productSliderParam = {
        controls: false,
        slideMargin: 0,
        item:1,
        addClass: "products-slider"
    }
    var $productSlider = $("#productsSlider");

    sliderInit( $productSlider, productSliderParam );

    $('#productsNext').click(() => {
        $productSlider.goToNextSlide();
    });

    $('#productsPrev').click(() => {
        $productSlider.goToPrevSlide();
    });

    var testimonialSliderParam = {
        addClass: "quotes-slider",
        controls: false,
        slideMargin: 70,
        adaptiveHeight: true,
        vertical: false,
        autoWidth: true,
        pager: false,
        item: 4,
        onSliderLoad: function() {
            $('#homepageSlider').removeClass('cS-hidden');
        }
    };

    var $homepageTetimonialsSlider = $('#homepageSlider');

    if ( $win.width() > 1200 ) {
        testimonialSliderParam.items = 1;
    }

    sliderInit( $homepageTetimonialsSlider, testimonialSliderParam );

} );

})(jQuery, window, document);

1 个答案:

答案 0 :(得分:0)

这是我发现的答案,似乎可以解决问题。我们正在结合使用CSS作为后备,效果很好。

// Find all YouTube videos
var $allVideos = $("iframe[src^='//player.vimeo.com'], 
iframe[src^='//www.youtube.com']"),


// The element that is fluid width
$fluidEl = $("body");

// Figure out and save aspect ratio for each video
$allVideos.each(function() {

$(this)
.data('aspectRatio', this.height / this.width)

// and remove the hard coded width/height
.removeAttr('height')
.removeAttr('width');

});

// When the window is resized
$(window).resize(function() {

var newWidth = $fluidEl.width();

// Resize all videos according to their own aspect ratio
$allVideos.each(function() {

var $el = $(this);
$el
  .width(newWidth)
  .height(newWidth * $el.data('aspectRatio'));

});

// Kick off one resize to fix all videos on page load
}).resize();

这是CSS解决方案:

<div class="videoWrapper">
<!-- Copy & Pasted from YouTube -->
<iframe width="560" height="349" src="http://www.youtube.com/embed/n_dZNLr2cME?rel=0&hd=1" frameborder="0" allowfullscreen></iframe>
</div>

.videoWrapper {
position: relative;
padding-bottom: 56.25%; /* 16:9 */
padding-top: 25px;
height: 0;
}
.videoWrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}