效果:

對於不規則圖片,在屏幕縮小時圖片適當左移,但為了不遮擋左側文字,左移至一定位置后圖片固定位置。
方法:
給背景圖片設置寬度和absolute定位,使得圖片浮於頁面。然后在js里邊判斷當前窗口大小,當頁面小到會致使圖片遮擋左側文字時,給圖片添加left屬性,這樣圖片就不會再向左移動了(如果文字在右側就添加right屬性)。
html
<body> <img class="backImg" src="img/backimg.png"> <div id="container"> </div> </body>
css
body { margin: 0px; min-width: 1920px; min-height: 800px; overflow: hidden; } .backImg { width: 1530px; position: absolute; right: 0; top: 0; } #container { position: fixed;
bottom: 250px;
width: 383px; display: flex; flex-direction: column; justify-content: center; margin-left: 131px; }
js
window.onload=function(){ if($(window).width()<=1660){ $('.backImg').attr('style','left:150px;'); } } $(window).resize(function(){ if($(window).width()<=1660){ $('.backImg').attr('style','left:150px;'); } else{ $('.backImg').removeAttr('style'); } })
