使用CSS部分重叠元素

时间:2015-06-30 01:58:49

标签: html css

我想部分重叠多个HTML元素 - 比如DIV - 如下图所示。黑色边框(Hanafuda卡片)代表我想要重叠的元素。

enter image description here

使用Javascript我确定我能想出一些东西,但我想知道是否有一个纯CSS解决方案。我考虑了相对定位,但问题是每张卡沿x轴需要越来越大的偏移量。

理想情况下,我希望重叠程度取决于有多少空间,以便元素在拥挤时聚集在一起,但这是次要的,我不介意使用JS来实现它。

3 个答案:

答案 0 :(得分:4)

你可以使用flex实现这一点,使除最后一张之外的所有牌都调整为剩余空间

以下是fiddle以下内容:



.container {
  display: flex;
  width: 300px;
}
.card-container {
  flex: 1 0 0;
  overflow-x: hidden;
}
.card-container:last-child {
  flex: 0 0 auto;
}
.card {
  background: linear-gradient(to right, #ccc 0%, #444 100%);
  width: 100px;
  height: 150px;
}

<div class="container">
  <div class="card-container">
    <div class="card"></div>
  </div>
  <div class="card-container">
    <div class="card"></div>
  </div>
  <div class="card-container">
    <div class="card"></div>
  </div>
  <div class="card-container">
    <div class="card"></div>
  </div>
  <div class="card-container">
    <div class="card"></div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

这也可以使用display: table来实现,display: table目前享有更高的浏览器兼容性,而不是更好的,灵活的Flex规范。

兼容性: IE8+ and all modern browsers.

  • 外部div被赋予display: table-cell

  • 每张图片都包含在table-layout: fixed

  • 的div中
  • .cards { display: table; table-layout: fixed; width: 50%; max-width: 700px; } .cards > div { display: table-cell; width: 100px; } .cards > div > img { display: block; }允许“单元格”重叠

  • 外部div可以保持灵活性,以允许图像根据剩余的空间更多/更少重叠

完整示例

<div class="cards">
  <div><img src="http://www.placehold.it/200x300" /></div>
  <div><img src="http://www.placehold.it/200x300/FF0000" /></div>
  <div><img src="http://www.placehold.it/200x300" /></div>
  <div><img src="http://www.placehold.it/200x300/FF0000" /></div>
</div>
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.*;

public class Main extends JFrame /*implements Runnable*/{

    static double clickCount = 0D;
    static double cpst = 0D;
    static JFrame frameMain = new JFrame("Clicker - Evan");
    static JLabel labelCount = new JLabel("Click Count: " + clickCount);
    static JLabel labelcps = new JLabel("Click Per Second: " + cpst);
    static ArrayList<AutoClicker> clickers = new ArrayList<AutoClicker>(/*Arrays.asList(AutoClicker("Mouse", 1, 10))*/);
    static ArrayList<JButton> buttonAuto = new ArrayList<JButton>();

    static JButton buttonMain = new JButton("Click");

    private static boolean running = false;
    //private Thread thread;

    public static void main(String[] args){
        init();
        run();
    }

    static long lastTime2 = System.nanoTime();
    final static double amountOfTicks2 = 2D;
    static double ns2 = 1000000000 / amountOfTicks2;
    double delta2 = 0;

    //Reference: https://www.youtube.com/watch?v=jEXxaPQ_fQo
    static class buttonMainAction implements ActionListener{
        public void actionPerformed(ActionEvent e){
            clickCount++;

            System.out.println("refresh?");


        }
    }
    static class buttonBuyAction implements ActionListener{
        public void actionPerformed(ActionEvent e){
            int fromNumber = Integer.parseInt(e.getActionCommand());

            clickCount -= (int)clickers.get( fromNumber ).getCost();
            clickers.get( fromNumber ).buy();
            buttonAuto.get( fromNumber ).setText(clickers.get( fromNumber ).getNumberOwned() 
                    + " " + clickers.get( fromNumber ).getName() + "(s) - Cost: " + (int)clickers.get( fromNumber ).getCost() + " Clicks");

            System.out.println("+1 " + clickers.get( fromNumber ).getName());
        }
    }


    //Source: https://www.youtube.com/watch?v=qSdKBQMdlLM
    //Source 2: https://www.youtube.com/watch?v=vFRuEgEdO9Q&list=PLah6faXAgguMnTBs3JnEJY0shAc18XYQZ&index=4
    public static void run(){

        long lastTime = System.nanoTime();
        final double amountOfTicks = 60D;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;

        //init();

        while(running){
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            if(delta >= 1){
                tick();
                delta--;
            }

            render();
        }
    }

    private static void init(){
        clickers.add(new AutoClicker("Mouse", .1, 10));
//      clickers.add(new AutoClicker("Monkey", .36, 50));
//      clickers.add(new AutoClicker("Toddler", .5, 250));
//      clickers.add(new AutoClicker("Pre-Teen", 1, 700));
//      clickers.add(new AutoClicker("Angsty Teen", 1.5, 1500));
//      clickers.add(new AutoClicker("Intern", 2.5, 2000));
//      clickers.add(new AutoClicker("Newbie", 5, 4000));
//      clickers.add(new AutoClicker("HR Staffer", 10, 10000));
//      clickers.add(new AutoClicker("Misc. Staffer", 15, 12000));
//      clickers.add(new AutoClicker("Media Analyst", 30, 100000));
//      clickers.add(new AutoClicker("CEO", 50, 800000));
//      clickers.add(new AutoClicker("Researcher", 80, 2000000));
//      
//      
//      clickers.add(new AutoClicker("Sacrifice Leader", 500, 99999999));

        System.out.println("clickers.size()=\t" + clickers.size());

        frameMain.setVisible(true);

        frameMain.setSize(800, 600);
        frameMain.setResizable(false);
        frameMain.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frameMain.setLayout(new FlowLayout());


        frameMain.add(buttonMain);
        buttonMain.addActionListener(new buttonMainAction() );


        buttonAuto.add(new JButton("0 Mouse(s) - Cost: 10 Clicks"));
//      buttonAuto.add(new JButton("0 Monkey(s) - Cost: 50 Clicks"));
//      buttonAuto.add(new JButton("0 Toddler(s) - Cost: 250 Clicks"));
//      buttonAuto.add(new JButton("0 Pre-Teen(s) - Cost: 700 Clicks"));
//      buttonAuto.add(new JButton("0 Angsty Teen(s) - Cost: 1500 Clicks"));
//      buttonAuto.add(new JButton("0 Intern(s) - Cost: 2000 Clicks"));
//      buttonAuto.add(new JButton("0 Newbie(s) - Cost: 4000 Clicks"));
//      buttonAuto.add(new JButton("0 HR Staffer(s) - Cost: 10000 Clicks"));
//      buttonAuto.add(new JButton("0 Misc. Staffer(s) - Cost: 12000 Clicks"));
//      buttonAuto.add(new JButton("0 Media Analyst(s) - Cost: 100000 Clicks"));
//      buttonAuto.add(new JButton("0 CEO(s) - Cost: 800000 Clicks"));
//      buttonAuto.add(new JButton("0 Researcher(s) - Cost: 2000000 Clicks"));
//      buttonAuto.add(new JButton("0 Sacrafice Leader(s) - Cost: 99999999 Clicks"));

        frameMain.add(labelcps);
        frameMain.add(labelCount);

        for(int i = 0; i<buttonAuto.size(); i++){
            buttonAuto.get(i).addActionListener(new buttonBuyAction() );
        buttonAuto.get(i).setActionCommand(Integer.toString(i));
        frameMain.add(buttonAuto.get(i));
        }


        running = true;
    }

    /*
    public synchronized void start(){
        if(running)
            return;
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    public synchronized void stop(){
        if(!running)
            return;
        running = false;

        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    */

    static void tick(){
        //do stuff
        for(AutoClicker clicker: clickers){
            clickCount += (clicker.getCPS() / 60)* clicker.getNumberOwned();
            System.out.println("added " + (clicker.getCPS() / 60)* clicker.getNumberOwned()
                    + " from " + clicker.getNumberOwned() + " " + clicker.getName() +"'s");
        }
    }
    static void render(){
        double cpst = 0;
        for(AutoClicker clicker: clickers){
            cpst += (clicker.getCPS())* clicker.getNumberOwned();
        }
        labelCount.setText("[ Click Count: " + (int)clickCount + " ]");
        labelcps.setText("[ Click Per Second: " + ((int)(cpst*10))/10.0 + " ]");

        for(int i = 0; i < clickers.size(); i++){
            if((int)clickers.get(i).getCost()>clickCount){
                if(buttonAuto.get(i).isEnabled())
                    buttonAuto.get(i).setEnabled(false);
            }else{
                if(!buttonAuto.get(i).isEnabled())
                    buttonAuto.get(i).setEnabled(true);
            }

        }
    }

}

答案 2 :(得分:1)

我发现最简单的解决方案是将 float: left 与否定 right-margin 结合使用:

.card {
  --card-width: 100px;

  margin-right: calc(25px - var(--card-width));
  float: left;
  
  width: var(--card-width);
  height: calc(var(--card-width) * 1.5);
  border: 1px solid black;
  background: white;
}
<div class="container">
  <div class="card"></div>
  <div class="card"></div>
  <div class="card"></div>
  <div class="card"></div>
</div>