【问题标题】:HTML Height/Width Attribute Overwrite for Small Screens小屏幕的 HTML 高度/宽度属性覆盖
【发布时间】:2020-01-02 08:46:09
【问题描述】:

我正在为一个项目嵌入 Addpipe 记录器,它在 HTML 中采用 Height 和 Width 属性,但这会将其设置为固定大小,并且对于较小的屏幕它不会按比例缩小。有没有办法覆盖 CSS 或 JavaScript 中的那些设置属性?

我尝试将 Height 和 Width 属性值设置为百分比和自动,但记录器消失了。它必须是4:316:9 比率。该网站使用媒体查询进行响应。对于小于 800 像素的屏幕,我需要更改 Piperecorder 的尺寸。我是一名初学者网页设计师,非常感谢任何帮助。谢谢!

您可以在此处查看到目前为止的外观:https://fewnew.github.io/YNWA-web

<!-- begin video recorder code -->
<link rel="stylesheet" href="//cdn.addpipe.com/2.0/pipe.css" />
<script type="text/javascript" src="//cdn.addpipe.com/2.0/pipe.js"></script>
<piperecorder id="custom-id" pipe-width="640" pipe-height="390" pipe-qualityurl="avq/360p.xml" pipe-accountHash="22864bed1e4827f6798d501706aeb89f" pipe-eid="1" pipe-mrt="120"></piperecorder>
<!-- end video recorder code -->

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    您可以通过一些简单的步骤获得#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部分,我做了一些修改

    【讨论】:

    • 嗨,阿里。非常感谢您对我的问题的答复。我对 jQuery 没有任何经验,所以我只是复制和粘贴,看看能否得到我想要的结果。我仍然对一部分感到困惑:我为 Piperecorder 标签中的身高和体重属性设置了哪些值?谢谢。
    • 不客气 :) 因为您的页面包含 jQuery,所以我提供了 jQuery 解决方案。上面的代码获取第一个视频标签的宽度和高度,并将其值应用于管道记录器。在这个解决方案中,您不需要手动设置任何值。例如$(sourceVideo).width(); 获取视频元素宽度并将其值存储到videoWidth 变量中,如您所见,我将其设置为styles 对象中的width 属性,然后通过jQuery 通过CSS 将样式应用于目标视频(piperecorder): $(targetVideo).css(styles);.
    • 如您所见,setVideoStyles 函数在文档就绪和调整大小的窗口中都使用,因此每次页面加载或用户调整页面大小时,该函数都会运行。该函数获取#video(视频ID)并将其应用于piperecorder。因此,您只需为视频 id 设置正确的样式即可修复 piperecorder 样式。因为现在它们将具有相同的样式。更多关于 jQuery CSS 的信息:api.jquery.com/css
    • 主要问题是这个标签:
    • 当我删除 pipe-width 和 pipe-height 属性时,记录器消失了。
    猜你喜欢
    • 2021-05-04
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 2013-12-24
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    相关资源
    最近更新 更多