首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何制作一个高度为100%的html-5横幅?

如何制作一个高度为100%的html-5横幅?
EN

Stack Overflow用户
提问于 2018-05-03 08:49:36
回答 1查看 89关注 0票数 0

我想做一个固定宽度(300像素)的横幅,但高度自适应(最小高度应保持600像素)。我在Adobe Animate中创建了我的横幅,并生成了此代码,但此函数可同时更改高度和宽度,并且不会使高度大于600px。请帮我更改一下这个函数

代码语言:javascript
复制
    function makeResponsive(isResp, respDim, isScale, scaleType) {      
    var lastW, lastH, lastS=1;      
    window.addEventListener('resize', resizeCanvas);        
    resizeCanvas();     
    function resizeCanvas() {           
        var w = lib.properties.width, h = lib.properties.height;            
        var iw = window.innerWidth, ih=window.innerHeight;          
        var pRatio = window.devicePixelRatio || 1, xRatio=iw/w, yRatio=ih/h, sRatio=1;          
        if(isResp) {                
            if((respDim=='width'&&lastW==iw) || (respDim=='height'&&lastH==ih)) {                    
                sRatio = lastS;                
            }               
            else if(!isScale) {                 
                if(iw<w || ih<h)                        
                    sRatio = Math.min(xRatio, yRatio);              
            }               
            else if(scaleType==1) {                 
                sRatio = Math.min(xRatio, yRatio);              
            }               
            else if(scaleType==2) {                 
                sRatio = Math.max(xRatio, yRatio);              
            }           
        }           
        canvas.width = w*pRatio*sRatio;         
        canvas.height = h*pRatio*sRatio;
        canvas.style.width = dom_overlay_container.style.width = anim_container.style.width =  w*sRatio+'px';               
        canvas.style.height = anim_container.style.height = dom_overlay_container.style.height = h*sRatio+'px';
        stage.scaleX = pRatio*sRatio;           
        stage.scaleY = pRatio*sRatio;           
        lastW = iw; lastH = ih; lastS = sRatio;            
        stage.tickOnUpdate = false;            
        stage.update();            
        stage.tickOnUpdate = true;      
    }
}
makeResponsive(true,'height',false,2);  
AdobeAn.compositionLoaded(lib.properties.id);
fnStartAnimation();

}

EN

回答 1

Stack Overflow用户

发布于 2018-05-03 10:34:30

更新

请试一下这个。此代码仅调整画布高度,不接触舞台。

代码语言:javascript
复制
function resizeCanvas() {         
    var w = lib.properties.width, h = lib.properties.height;            
    var iw = window.innerWidth, ih=window.innerHeight;          
    var pRatio = window.devicePixelRatio || 1, xRatio=iw/w, yRatio=ih/h, sRatio=1;          
    if(isResp) {          
        // added these 4 lines     
        if(yRatio>1)
            sRatio = yRatio;
        else
            sRatio = 1;

        // commented this out
        // if((respDim=='width'&&lastW==iw) || (respDim=='height'&&lastH==ih)) {                    
        //  sRatio = lastS;                
        // }                
        // else if(!isScale) {                  
        //  if(iw<w || ih<h)                        
        //      sRatio = Math.min(xRatio, yRatio);              
        // }                
        // else if(scaleType==1) {                  
        //  sRatio = Math.min(xRatio, yRatio);              
        // }                
        // else if(scaleType==2) {                  
        //  sRatio = Math.max(xRatio, yRatio);              
        // }            
    }           

        // canvas.width = w*pRatio*sRatio;          
        canvas.height = h*pRatio*sRatio;
        // canvas.style.width = dom_overlay_container.style.width = anim_container.style.width =  w*sRatio+'px';                
        canvas.style.height = anim_container.style.height = dom_overlay_container.style.height = h*sRatio+'px';
        // stage.scaleX = pRatio*sRatio;            
        // stage.scaleY = pRatio*sRatio;            
        lastW = iw; lastH = ih; lastS = sRatio;            
        stage.tickOnUpdate = false;            
        stage.update();            
        stage.tickOnUpdate = true;      
}

这段代码与Animate发布的代码相同,除了:

  • 添加了4 lines
  • Commented out IF block
  • Commented out canvas.width stage scaling

下面的旧答案

好的,谢谢你的信息。尝尝这个。我更新了"resizeCanvas()“函数:

代码语言:javascript
复制
function resizeCanvas() {           
        var w = lib.properties.width, h = lib.properties.height;            
        var iw = window.innerWidth, ih=window.innerHeight;          
        var pRatio = window.devicePixelRatio || 1, xRatio=iw/w, yRatio=ih/h, sRatio=1;          
        if(isResp) {          
            // added these 4 lines     
            if(yRatio>1)
                sRatio = yRatio;
            else
                sRatio = 1;

            // commented this out
            // if((respDim=='width'&&lastW==iw) || (respDim=='height'&&lastH==ih)) {                    
            //  sRatio = lastS;                
            // }                
            // else if(!isScale) {                  
            //  if(iw<w || ih<h)                        
            //      sRatio = Math.min(xRatio, yRatio);              
            // }                
            // else if(scaleType==1) {                  
            //  sRatio = Math.min(xRatio, yRatio);              
            // }                
            // else if(scaleType==2) {                  
            //  sRatio = Math.max(xRatio, yRatio);              
            // }            
        }           

        // canvas.width = w*pRatio*sRatio;          -- commented this out
        canvas.width = w*pRatio;                    // replaced with this
        canvas.height = h*pRatio*sRatio;
        // canvas.style.width = dom_overlay_container.style.width = anim_container.style.width =  w*sRatio+'px';            -- commented this out
        canvas.style.width = dom_overlay_container.style.width = anim_container.style.width =  w+'px';                      // replaced with this
        canvas.style.height = anim_container.style.height = dom_overlay_container.style.height = h*sRatio+'px';
        // stage.scaleX = pRatio*sRatio;        -- commented this out
        stage.scaleX = pRatio;                  // replaced with this
        stage.scaleY = pRatio*sRatio;           
        lastW = iw; lastH = ih; lastS = sRatio;            
        stage.tickOnUpdate = false;            
        stage.update();            
        stage.tickOnUpdate = true;      
    }

我在这里做了几件事:

  • 仅在yRatio (height)大于1时缩放,否则默认缩放比率(SRatio)为1。
  • 在width

上禁用缩放

如果这就是你要找的,请告诉我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50145424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档