我正在制作一个响应式背景视频。我有这个代码。
<video id="bgvideo" />
function scaleVideo() {
var windowHeight = $(window).height();
var windowWidth = $(window).width();
var videoNativeWidth = $('video#bgvideo')[0].videoWidth;
var videoNativeHeight = $('video#bgvideo')[0].videoHeight;
var heightScaleFactor = windowHeight / videoNativeHeight;
var widthScaleFactor = windowWidth / videoNativeWidth;
if (widthScaleFactor >= heightScaleFactor) {
var scale = widthScaleFactor;
} else {
var scale = heightScaleFactor;
}
var scaledVideoHeight = videoNativeHeight * scale;
var scaledVideoWidth = videoNativeWidth * scale;
$('video#bgvideo').height(scaledVideoHeight);
$('video#bgvideo').width(scaledVideoWidth);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 grunt 来编译我的代码等。grunt 的 Jshint 说我使用的“scale”超出了范围,我无法理解为什么。
有什么建议吗?

如果您想在语句外部使用它,则不应在语句var scale = heightScaleFactor;内部编写。else
scale外部初始化if
var scale;
if (widthScaleFactor >= heightScaleFactor) {
scale = widthScaleFactor;
} else {
scale = heightScaleFactor;
}
Run Code Online (Sandbox Code Playgroud)
小智 2
试试这个:
function scaleVideo() {
var scale; //this is the change
var windowHeight = $(window).height();
var windowWidth = $(window).width();
var videoNativeWidth = $('video#bgvideo')[0].videoWidth;
var videoNativeHeight = $('video#bgvideo')[0].videoHeight;
var heightScaleFactor = windowHeight / videoNativeHeight;
var widthScaleFactor = windowWidth / videoNativeWidth;
if (widthScaleFactor >= heightScaleFactor) {
scale = widthScaleFactor; //simply modify the value here
} else {
scale = heightScaleFactor;
}
var scaledVideoHeight = videoNativeHeight * scale;
var scaledVideoWidth = videoNativeWidth * scale;
$('video#bgvideo').height(scaledVideoHeight);
$('video#bgvideo').width(scaledVideoWidth);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7995 次 |
| 最近记录: |