【问题标题】:Drupal format Javascript to be included in theme将包含在主题中的 Drupal 格式 Javascript
【发布时间】:2015-09-17 17:12:42
【问题描述】:

我正在尝试在我的 Drupal 站点主页中实现 http://jsfiddle.net/mgmilcher/8R7Xx/sho/ 响应式 HTML5 视频背景,但没有成功。我相信这与 JS 格式不正确有关。这是我试图包含在我的 theme.info 文件中的自定义 JS,使用脚本 [] = custom.js。

格式化这个的正确方法是什么?目前这个处理的所有东西都没有显示出来。

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');


    });
}

【问题讨论】:

    标签: javascript jquery html drupal drupal-7


    【解决方案1】:

    你需要把所有的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 的
    • 还是一样...如果我在页面上的 中添加它--front.tpl.php 它工作正常...但显然这不是最佳实践。一旦我将它添加到 /js 文件夹中的 custom.js 并通过脚本 [] = js/custom.js 将其包含在 theme.info 文件中,所有内容都会消失。
    猜你喜欢
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    相关资源
    最近更新 更多