<div class="form">
<label for="object-fit">object-fit:</label>
<select name="object-fit" id="object-fit">
<option value="none">none</option>
<option value="fill">fill</option>
<option value="cover">cover</option>
<option value="contain">contain</option>
<option value="scale-down">scale-down</option>
</select>
</div>
<div class="container">
<img class="ltr" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123941/sunset.jpg" />
</div>
<div class="actions">
<div class="control">
<label for="position-x">object-position-x:</label>
<input type="range" id="position-x" name="position" min="-100" max="100" step="5" value="0" />
<span id="output__x--per"></span>
</div>
<div class="control">
<label for="position-y">objec-position-y:</label>
<input type="range" id="position-y" name="position" min="-100" max="100" step="5" value="0" />
<span id="output__y--per"></span>
</div>
</div>
@import url("https://fonts.googleapis.com/css?family=Gochi+Hand");
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
width: 100vw;
min-height: 100vh;
margin: 0;
background-color: #291642;
font-family: "Gochi Hand", sans-serif;
font-size: 100%;
letter-spacing: 0.1rem;
color: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background-blend-mode: hue;
}
.actions {
padding: 3vh;
display: flex;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.5);
width: 100%;
font-size: 1.5rem;
margin-top: auto;
}
.control {
margin: 0 5px;
}
.container {
width: 540px;
height: 480px;
border: 10px solid black;
background: #09f;
margin-top: auto;
position: relative;
}
img {
width: 100%;
height: 100%;
}
:root {
--position-x: 0;
--position-y: 0;
--object-fit: none;
}
img {
object-fit: var(--object-fit);
object-position: var(--position-x) var(--position-y);
}
.form {
width: 100%;
background-color: rgba(0, 0, 0, 0.5);
padding: 10px;
display: flex;
justify-content: center;
align-items: center;
font-sizee: 1.2rem;
}
View Compiled
const containerElement = document.querySelector(".container");
const imgElement = document.querySelector("img");
const objectFitSelectElement = document.getElementById("object-fit");
const imgElementRectWidth = imgElement.getBoundingClientRect().width;
const imgElementRectHeight = imgElement.getBoundingClientRect().height;
console.log(imgElementRectWidth);
const imgElementWidth = imgElement.naturalWidth;
const imgElementHeight = imgElement.naturalHeight;
const xRange = document.getElementById("position-x");
const yRange = document.getElementById("position-y");
const outputXPer = document.getElementById("output__x--per");
const outputYPer = document.getElementById("output__y--per");
const xRangeValue = xRange.value;
const yRangeValue = yRange.value;
outputXPer.textContent = `${xRangeValue}%`;
outputYPer.textContent = `${yRangeValue}%`;
const rootElement = document.documentElement;
xRange.addEventListener("change", (etv) => {
const rangeValue = etv.target.value;
rootElement.style.setProperty("--position-x", `${rangeValue}%`);
outputXPer.textContent = `${rangeValue}%`;
});
yRange.addEventListener("change", (etv) => {
const rangeValue = etv.target.value;
rootElement.style.setProperty("--position-y", `${rangeValue}%`);
outputYPer.textContent = `${rangeValue}%`;
});
objectFitSelectElement.addEventListener("change", (etv) => {
rootElement.style.setProperty("--object-fit", `${etv.target.value}`);
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.