"/>
首页 \ 问答 \ 按比例调整背景图像大小(Resizing background image proportionally)

按比例调整背景图像大小(Resizing background image proportionally)

我有一个容器div:

<div id="fullpage" class="fullpage_bg">
    ...rest of the elements
</div

它有纵向和横向的背景图像:

#fullpage {background-color: rgba(0,0,0,0.2); background-position: center center;}
@media screen and (orientation:portrait) {
    .fullpage_bg{
        background-image: url('images/bg_portrait.jpg');
    }
}
@media screen and (orientation:landscape) {
    .fullpage_bg{
        background-image: url('images/bg_landscape.jpg');
    }
}

对于不同的屏幕尺寸,有没有办法按比例缩放图像,使用CSS或纯JS(无jQuery)? 例如:对于横向图像,图像可能占据屏幕的整个高度,但两侧仍有一些边距(因为图像必须保持比例,但仍尽可能地填满屏幕)。 在纵向中,它可能需要整个宽度,但在顶部和底部有一些边距。 谢谢!


I have a container div:

<div id="fullpage" class="fullpage_bg">
    ...rest of the elements
</div

It has a background image for portrait and landscape:

#fullpage {background-color: rgba(0,0,0,0.2); background-position: center center;}
@media screen and (orientation:portrait) {
    .fullpage_bg{
        background-image: url('images/bg_portrait.jpg');
    }
}
@media screen and (orientation:landscape) {
    .fullpage_bg{
        background-image: url('images/bg_landscape.jpg');
    }
}

Is there a way to scale the image proportionally, either by using CSS or pure JS (no jQuery), for different screen sizes? For example: for landscape the image might take the entire height of the screen, but still have some margins on the sides (because the image must maintain proportions, but still fill the screen as much as possible). In portrait it might take the entire width, but have some margins on top and bottom. Thanks!


原文:https://stackoverflow.com/questions/28584635
更新时间:2021-10-18 20:10

最满意答案

似乎它不是关于运行一个函数, 如果另一个函数是成功的 ,但是在异步操作完成之后 。 在calcRoute ,路由计算API调用是异步的; 这意味着函数将 directionsService完成创建路径之前返回。 一种解决方案是修改calcRoute以进行回调:

function calcRoute(callback) {
    var start = document.getElementById('start').value;
    var end = "Via Tiburtina 500, Roma";
    var request = {
        origin:start,
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.TRANSIT
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);

        // If a callback was passed, run it
        if(typeof callback === "function") {
            callback();
        }

      }
    });
}

然后更改函数调用以传递回调:

<form onsubmit="calcRoute(function(){ boxappear(); reducemap(); }); return false">

旁注:我不建议在HTML属性中内联代码,混合结构和行为,也可能变得难以维护; 我建议调查将事件附加到DOM元素的其他方法。


It seems it's not about running one function if the other one is succesful, but after an asynchronous operation is done. In calcRoute, the route calculation API call is asynchronous; that means the function will return before the directionsService finishes creating the route. One solution is to modify calcRoute to take a callback:

function calcRoute(callback) {
    var start = document.getElementById('start').value;
    var end = "Via Tiburtina 500, Roma";
    var request = {
        origin:start,
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.TRANSIT
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);

        // If a callback was passed, run it
        if(typeof callback === "function") {
            callback();
        }

      }
    });
}

Then change your function call to pass the callback:

<form onsubmit="calcRoute(function(){ boxappear(); reducemap(); }); return false">

Side note: I wouldn't recommend inlining code in HTML attributes like that, is mixes structure and behavior, and can also become hard to maintain; I suggest looking into other ways of attaching events to DOM elements.

相关问答

更多
  • 删除onSubmit="" ,然后使用jQuery拦截提交: $("#myform").on("submit", function() { validate(); if(is_good) { redirect(); } return false; }); 然后,您可以根据需要运行任意数量的功能。 这是一个简单的演示: https : //jsfiddle.net/johnniebenson/c2n4gwjj/ Remove onSubmit="" from
  • 你可以做这样的事情 function validate(button_value) { if(button_value=='test1') { if(document.getElementById('rest1').value=="") { alert('Blank for input 2'); return false; } ...
  • 将onClick代码放在与onSumbit代码相同的函数中。 UPDATE 在onClick代码的末尾,您return false; ,这会停止事件的正常传播并停止onSubmit事件的触发。 因此,如果您希望提交按钮提交表单,请删除return false; 从它的onClick处理程序。 当您单击提交按钮时,您将触发按钮上的click事件以及嵌套按钮的表单上的submit事件(除非您停止使用return false;等事件传播事件)。 所以你真的只需要一个submit事件处理程序来完成两个当前处理程序的 ...
  • 似乎它不是关于运行一个函数, 如果另一个函数是成功的 ,但是在异步操作完成之后 。 在calcRoute ,路由计算API调用是异步的; 这意味着函数将在 directionsService完成创建路径之前返回。 一种解决方案是修改calcRoute以进行回调: function calcRoute(callback) { var start = document.getElementById('start').value; var end = "Via Tiburtina 500, Rom ...
  • 使用命名函数。 function Ajax1() { /* your ajax stuff */ } function Ajax2() { /* second ajax stuff */ } 然后你可以从Ajax1的成功部分调用Ajax2。 Solved it with the following, not sure what I was doing wrong previously: