您可以通过一些简单的步骤获得#video 的大小,然后使用 jQuery 将其应用于记录器样式:
首先,创建一个 IFEE 并将 jquery $ 传递给它以防止 JS 冲突:
(function($){
// next steps goes here...
})(jQuery);
然后创建一个函数来获取源视频样式,以便在以后的步骤中将其应用于目标视频:
(function($){
// We set a name "setVideoStyles" to function to call it later with
// this name.
// We pass 2 arguments to it: sourceVideo and targetVideo
// Later we pass '#video' as sourceVideo and '.pipeRecordRTC' as targetVideo
function setVideoStyles(sourceVideo, targetVideo) {
// Get sourceVideo (#video) width and height
var videoWidth = $(sourceVideo).width();
var videoHeight = $(sourceVideo).height();
// Create an object with #video current styles
// and set our variables to CSS, width and height are CSS attributes
// which we can apply with jQuery
var styles = {
width: videoWidth,
height: videoHeight
}
// Here we pass styles object as CSS method values with jQuery
// for the target video
$(targetVideo).css(styles);
}
})(jQuery);
现在我们可以将这个函数用于我们所有的目的:
(function($){
function setVideoStyles(sourceVideo, targetVideo) {
var videoWidth = $(sourceVideo).width();
var videoHeight = $(sourceVideo).height();
var styles = {
width: videoWidth,
height: videoHeight
}
// Here we get screen width on each resize
var windowWidth = $(window).width();
// We check if screen is equal or smaller than 800px
if(windowWidth <= 800) {
$(targetVideo).css(styles);
}
}
// Now we use document ready to apply the styles when page loads
$(document).ready(function() {
setVideoStyles('#video', '.pipeRecordRTC');
});
// And window on resize to apply when the user resize the window
$(window).on('resize', function() {
setVideoStyles('#video', '.pipeRecordRTC');
});
})(jQuery);
请随时提出任何问题。
注意:请注意我添加了一个我之前忘记的 if 语句。
注意二:请查看IFEE部分,我做了一些修改