Drupal格式Javascript将包含在主题中
我试图在我的Drupal网站主页中实现响应性HTML5视频背景,但没有成功。我相信这与JS的格式不正确有关。下面是我试图使用script[]=custom.JS在我的theme.info文件中包含的自定义JS 设置此格式的正确方式是什么?目前,此处理的所有内容都未显示Drupal格式Javascript将包含在主题中,javascript,jquery,html,drupal,drupal-7,Javascript,Jquery,Html,Drupal,Drupal 7,我试图在我的Drupal网站主页中实现响应性HTML5视频背景,但没有成功。我相信这与JS的格式不正确有关。下面是我试图使用script[]=custom.JS在我的theme.info文件中包含的自定义JS 设置此格式的正确方式是什么?目前,此处理的所有内容都未显示 jQuery(document).ready(function ($) { // Resive video scaleVideoContainer(); initBannerVideoSize('.vi
jQuery(document).ready(function ($) {
// Resive video
scaleVideoContainer();
initBannerVideoSize('.video-container .poster img');
initBannerVideoSize('.video-container .filter');
initBannerVideoSize('.video-container video');
$(window).on('resize', function() {
scaleVideoContainer();
scaleBannerVideoSize('.video-container .poster img');
scaleBannerVideoSize('.video-container .filter');
scaleBannerVideoSize('.video-container video');
});
});
/** Reusable Functions **/
/********************************************************************/
function scaleVideoContainer() {
var height = $(window).height();
var unitHeight = parseInt(height) + 'px';
$('.homepage-hero-module').css('height',unitHeight);
}
function initBannerVideoSize(element){
$(element).each(function(){
$(this).data('height', $(this).height());
$(this).data('width', $(this).width());
});
scaleBannerVideoSize(element);
}
function scaleBannerVideoSize(element){
var windowWidth = $(window).width(),
windowHeight = $(window).height(),
videoWidth,
videoHeight;
console.log(windowHeight);
$(element).each(function(){
var videoAspectRatio = $(this).data('height')/$(this).data('width'),
windowAspectRatio = windowHeight/windowWidth;
if (videoAspectRatio > windowAspectRatio) {
videoWidth = windowWidth;
videoHeight = videoWidth * videoAspectRatio;
$(this).css({'top' : -(videoHeight - windowHeight) / 2 + 'px', 'margin-left' : 0});
} else {
videoHeight = windowHeight;
videoWidth = videoHeight / videoAspectRatio;
$(this).css({'margin-top' : 0, 'margin-left' : -(videoWidth - windowWidth) / 2 + 'px'});
}
$(this).width(videoWidth).height(videoHeight);
$('.homepage-hero-module .video-container video').addClass('fadeIn animated');
});
}
您需要将所有js代码包装在
(function ($) {
...
})(jQuery);
因此,custom.js的内容应该如下所示:
(function ($) {
$(document).ready(function() {
// Resive video
scaleVideoContainer();
initBannerVideoSize('.video-container .poster img');
initBannerVideoSize('.video-container .filter');
initBannerVideoSize('.video-container video');
});
$(window).on('resize', function() {
scaleVideoContainer();
scaleBannerVideoSize('.video-container .poster img');
scaleBannerVideoSize('.video-container .filter');
scaleBannerVideoSize('.video-container video');
});
/** Reusable Functions **/
/********************************************************************/
function scaleVideoContainer() {
var height = $(window).height();
var unitHeight = parseInt(height) + 'px';
$('.homepage-hero-module').css('height',unitHeight);
}
function initBannerVideoSize(element){
$(element).each(function(){
$(this).data('height', $(this).height());
$(this).data('width', $(this).width());
});
scaleBannerVideoSize(element);
}
function scaleBannerVideoSize(element){
var windowWidth = $(window).width(),
windowHeight = $(window).height(),
videoWidth,
videoHeight;
console.log(windowHeight);
$(element).each(function(){
var videoAspectRatio = $(this).data('height')/$(this).data('width'),
windowAspectRatio = windowHeight/windowWidth;
if (videoAspectRatio > windowAspectRatio) {
videoWidth = windowWidth;
videoHeight = videoWidth * videoAspectRatio;
$(this).css({'top' : -(videoHeight - windowHeight) / 2 + 'px', 'margin-left' : 0});
} else {
videoHeight = windowHeight;
videoWidth = videoHeight / videoAspectRatio;
$(this).css({'margin-top' : 0, 'margin-left' : -(videoWidth - windowWidth) / 2 + 'px'});
}
$(this).width(videoWidth).height(videoHeight);
$('.homepage-hero-module .video-container video').addClass('fadeIn animated');
});
}
})(jQuery);
谢谢,不幸的是,当我尝试包含此文档时,它格式化的所有类都不会显示…根本不会。如果我将这个js添加到html上的容器中,信息会显示得很好。所以我认为问题在于从外部js中包含它。仍然是一样的…如果我在页面--front.tpl.php中添加它,它工作得很好…但显然这不是最佳实践。一旦我将它添加到/js文件夹中的custom.js并通过脚本[]=js/custom.js将其包含在theme.info文件中,所有内容都将消失。