自动缩放图像以适应设备高度的其余部分

时间:2016-04-02 20:55:13

标签: javascript html css html5 css3

首先,这是我要寻找的图像:

enter image description here

我想在顶部放置一个标题,在标题下面放置一个段落,在段落下面放置一个SVG图像。我希望这能在任何尺寸的设备上正确扩展(320x480是我要去的最小的iPhone4大小的设备)。

我有以下代码:

<html>
    <head>
          <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    </head>
    <body>
        <div style="height:100%;position:absolute;top:0;left:0;bottom:0;right:0;">
            <div>
                <h2>Title</h2>
                <p>Some long paragraph will be inserted here</p>
            </div>
            <div>
                <img src="mySvg.svg" /> <!-- This image should scale to fit the remaining of the screen size. -->
            </div>
        </div>
    </body>
</html>

问题是在小型设备上有滚动条,您需要向下滚动才能查看图像的其余部分。我希望它能够正确缩放,以便它完全适合屏幕,因此不需要滚动。

编辑:我正在使用框架,作为框架的结果,我无法编辑HTML或BODY标签

3 个答案:

答案 0 :(得分:3)

尝试使用flexbox +位置技巧。

<强> jsFiddle

&#13;
&#13;
.container {
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  flex-direction: column;
}
.container div:last-child {
  flex: 1;
  position: relative;
}
.container div:last-child img {
  position: absolute;
  max-width: 100%;
  max-height: 100%;
}
&#13;
<div class="container">
  <div>
    <h2>Title</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
  </div>
  <div>
    <img src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/w3c.svg" />
  </div>
</div>
&#13;
&#13;
&#13;

或者,使用尺寸包含的Flexbox +背景图片。

<强> jsFiddle

&#13;
&#13;
.container {
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  flex-direction: column;
}
.container div:last-child {
  flex: 1;
  background: url("https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/w3c.svg") 0 0 no-repeat;
  background-size: contain;
}
&#13;
<div class="container">
  <div>
    <h2>Title</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
  </div>
  <div></div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我刚刚给了身体一个最小高度和最小宽度,就像它从来没有比电话更小:

&#13;
&#13;
.test{
  min-height:480px;
  min-width: 320px;
  background-color: pink;
  text-align:center;
  display: flex;
  flex-direction:column;
  justify-content: flex-start;
  align-items: center;
  padding: 0px 10px;
}
 body{
  margin:0px;
}
&#13;
 <body>
        <div class="test" style="height:100%;position:relative;top:0;left:0;bottom:0;right:0;">

            <div class="para">
                <h2>Title</h2>
                <p>Some long paragraph will be inserted hereSome long paragraph will be inserted hereSome long paragraph will be inserted hereSome long paragraph will be inserted hereSome long paragraph will be inserted hereSome long paragraph will be inserted hereSome long paragraph will be inserted hereSome long paragraph will be inserted hereSome long paragraph will be inserted hereSome long paragraph will be inserted hereSome long paragraph will be inserted hereSome long paragraph will be inserted hereSome long paragraph will be inserted hereSome long paragraph will be inserted hereSome long paragraph will be inserted here</p>
            </div>

            <div class="test">
 
                <img src="http://lorempixel.com/71/95" /> <!-- This image should scale to fit the remaining of the screen size. -->

            </div>

        </div>

    </body>
&#13;
&#13;
&#13;

答案 2 :(得分:-1)

package com.example.quiz; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.os.CountDownTimer; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Random; import java.util.concurrent.TimeUnit; import java.util.logging.Handler; public class CS_Assembly_Easy extends Activity{ TextView topscore; public static final String mypreference = "mypref"; public static final String Topscore = "topKey"; ArrayList<Question> quesList; ArrayList<Question> toSelectFrom; // <--- HERE int score = 0; int qid = 0; int lives = 5; int round = 1; int timer; Question currentQ; TextView txtQuestion, times, scored, livess, rounds; Button button1, button2, button3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cs_assembly_easy); CS_Assembly_QuizHelper db = new CS_Assembly_QuizHelper(this); // my question bank class quesList = db.getAllQuestions(); toSelectFrom = new ArrayList<Question>(); // <--- HERE toSelectFrom.addAll(quesList); // <--- HERE Random random = new Random();// this will fetch all quetonall questions currentQ = toSelectFrom.get( random.nextInt(toSelectFrom.size())); // the current question <-- edited here too. toSelectFrom.remove(toSelectFrom.indexOf(currentQ)); // <--- HERE txtQuestion = (TextView) findViewById(R.id.txtQuestion); // the textview in which the question will be displayed // the three buttons, // the idea is to set the text of three buttons with the options from question bank button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button3 = (Button) findViewById(R.id.button3); // the textview in which will be displayed scored = (TextView) findViewById(R.id.score); // the timer times = (TextView) findViewById(R.id.timers); rounds = (TextView) findViewById(R.id.round); // method which will set the things up for our game setQuestionView(); times.setText("00:02:00"); // A timer of 60 seconds to play for, with an interval of 1 second (1000 milliseconds) CounterClass timer = new CounterClass(60000, 1000); timer.start(); // button click listeners button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // passing the button text to other method // to check whether the anser is correct or not // same for all three buttons getAnswer(button1.getText().toString()); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getAnswer(button2.getText().toString()); } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getAnswer(button3.getText().toString()); } }); topscore = (TextView) findViewById(R.id.Topscore); sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE); if (sharedpreferences.contains(topscore) > score) { topscore.setText(sharedpreferences.getString(topscore, "")); } } public void Save(View view) { String n = topscore.getText().toString(); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Topscore, n); editor.commit(); } public void clear(View view) { topscore = (TextView) findViewById(R.id.Topscore); topscore.setText(""); } public void Get(View view) { topscore = (TextView) findViewById(R.id.Topscore); sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE); if (sharedpreferences.contains(topscore)) { topscore.setText(sharedpreferences.getString(topscore, "")); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void getAnswer(String AnswerString) { if (currentQ.getANSWER().equals(AnswerString)) { // if conditions matches increase the int (score) by 1 // and set the text of the score view score++; scored.setText(" Score : " + score); LayoutInflater inflater = getLayoutInflater(); // Inflate the Layout View layout = inflater.inflate(R.layout.correct, (ViewGroup) findViewById(R.id.custom_toast_layout)); TextView text = (TextView) layout.findViewById(R.id.textToShow); // Set the Text to show in TextView text.setText("CORRECT"); text.setTextSize(25); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.BOTTOM, 0, 50); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(layout); toast.show(); } else if(lives > -10){ LayoutInflater inflater = getLayoutInflater(); // Inflate the Layout View layout = inflater.inflate(R.layout.wrong, (ViewGroup) findViewById(R.id.custom_toast_layout)); TextView text = (TextView) layout.findViewById(R.id.textToShow); // Set the Text to show in TextView text.setText("WRONG"); text.setTextSize(25); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.BOTTOM, 0, 50); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(layout); toast.show(); } else { Intent intent = new Intent(CS_Assembly_Easy.this, ResultActivity_Assembly_Easy.class); Bundle b = new Bundle(); b.putInt("score", score); // Your score intent.putExtras(b); // Put your score to your next startActivity(intent); finish(); } { } if (qid < 10) { // if questions are not over then do this Random random = new Random(); currentQ = toSelectFrom.get(random.nextInt(toSelectFrom.size())); // <<--- HERE toSelectFrom.remove(toSelectFrom.indexOf(currentQ)); // <<--- AND HERE setQuestionView(); round++; rounds.setText(" Question:" + round + "/10"); } else { // if over do this Intent intent = new Intent(CS_Assembly_Easy.this, ResultActivity_Assembly_Easy.class); Bundle b = new Bundle(); b.putInt("score", score); // Your score intent.putExtras(b); // Put your score to your next startActivity(intent); finish(); } } @TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") public class CounterClass extends CountDownTimer { public CounterClass(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); // TODO Auto-generated constructor stub } @Override public void onFinish() { times.setText("Time is up"); } @Override public void onTick(long millisUntilFinished) { // TODO Auto-generated method stub long millis = millisUntilFinished; String hms = String.format( "%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS .toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS .toMinutes(millis))); System.out.println(hms); times.setText(hms); } } private void setQuestionView() { // the method which will put all things together txtQuestion.setText(currentQ.getQUESTION()); button1.setText(currentQ.getOPTA()); button2.setText(currentQ.getOPTB()); button3.setText(currentQ.getOPTC()); qid++; } @Override public void onBackPressed() { return; } } ,标题<img><div>段标记为<div>所有相同的名称,并使用这样的JavaScript。

&#13;
&#13;
class
&#13;
&#13;
&#13;