<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #container { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); transition: all 0.5s; width: 1280px; height: 500px; background-image: url("img/03.jpg"); overflow: hidden; } /*使用css创建html before向左 after向右 是行内*/ #container::before, #container::after { float: left; content: ""; width: 50%; height: 500px; background-image: url("img/01.jpg"); background-size: cover; transition: all 0.5s; } #container::after { content: ""; background-position: right 0; } #container:hover::before { transform: translateX(-100%); } #container:hover::after { transform: translateX(100%); } </style> </head> <body> <div id="container"> </div> </body> </html>