Javascript 如何制作100%高度的html-5横幅?
我想做一个固定宽度(300像素)的横幅,但自适应高度(最小高度应保持600像素)。我在Adobe Animate中创建了我的横幅,并且生成了此代码,但此函数会更改高度和宽度,并且不会使高度大于600 px。 请帮我更改此功能Javascript 如何制作100%高度的html-5横幅?,javascript,Javascript,我想做一个固定宽度(300像素)的横幅,但自适应高度(最小高度应保持600像素)。我在Adobe Animate中创建了我的横幅,并且生成了此代码,但此函数会更改高度和宽度,并且不会使高度大于600 px。 请帮我更改此功能 function makeResponsive(isResp, respDim, isScale, scaleType) { var lastW, lastH, lastS=1; window.addEventListener
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();
函数makeResponsive(isResp、respDim、isScale、scaleType){
var lastW,lastH,lastS=1;
addEventListener('resize',resizeCanvas);
调整画布的大小();
函数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;
如果(isResp){
如果((respDim='width'和&lastW==iw)| |(respDim='height'和&lastH==ih)){
sRatio=持续时间;
}
如果(!isScale){
如果(iw)更新
请改为尝试此操作。此代码仅调整画布高度,不接触舞台
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;
}
函数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;
如果(isResp){
//添加了这4行
如果(yRatio>1)
sRatio=yRatio;
其他的
sRatio=1;
//我把这评论掉了
//如果((respDim='width'和&lastW==iw)| |(respDim='height'和&lastH==ih)){
//sRatio=持续时间;
// }
//如果(!isScale){
//if(IW)你只能用CSS来实现。只需设置width=“300px”,min height=“600px”。我不确定你想如何使其自适应。此函数由Adobe Animate生成,我在其中选中了“使响应”框只有高度。但它也会改变宽度。我不知道Adobe Animate是如何工作的,如果你用JavaScript找不到答案,我建议使用CSS。预期的结果是什么?宽度固定在300px,但高度最小为600px?是的,这个横幅的最小高度应该保持在600px,但高度应该可以缩放谢谢uch!你帮了我很多,但是现在横幅的内容被拉伸到了高度,我需要内容保留在中间,横幅顶部和底部的空间填充背景啊,这意味着横幅本身不应该缩放?我对你最初问题的理解是“此横幅的最小高度应保持600像素,但高度应可缩放”。您能帮助我更好地理解它吗?谢谢!您的意思是始终将横幅置于浏览器的中心,但横幅尺寸本身固定为300x600?仅宽度(300)是固定的。高度应为100%,可缩放,但只有背景画布,而不是内容。更新了答案。不确定这是否是您要找的。
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;
}