2 回答
TA贡献1864条经验 获得超6个赞
function passTeste(arg) {
const element = document.getElementById("mainImage-teste-js")
const transformValue = window.getComputedStyle(element).transform;
var matrix = new WebKitCSSMatrix(element.webkitTransform);
console.log(element.style.transform) // Returns empty value
console.log(matrix); // Returns the matrix
console.log(transformValue); // Returns => matrix(1, 0, 0, 1, -1352, 0)
}
.container {
width: 200px;
overflow: hidden;
}
.product__image--mainImage {
display: flex;
transition: 2s;
transform: translate(-200%);
}
.product__image--mainImage img {
max-width: 100%;
}
.container > p {
display: flex;
justify-content: space-between;
font-size: 45px;
cursor: pointer;
}
<div id="mainImage-teste-js" class="container">
<div class="product__image--mainImage">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTOues59dCMXRYVPt6H8XvWGAuPlwHBprFWEw&usqp=CAU" />
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSUEwJ_CKwAt4M1k_UTZSUCVUTy2XWjWLdK3w&usqp=CAU" />
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTOues59dCMXRYVPt6H8XvWGAuPlwHBprFWEw&usqp=CAU" />
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSUEwJ_CKwAt4M1k_UTZSUCVUTy2XWjWLdK3w&usqp=CAU" />
</div>
<p onclick="passTeste()">></p>
</div>
TA贡献1757条经验 获得超8个赞
你几乎很好,你只需要应用计算值来WebKitCSSMatrix读取m41,因为变换是基于元素尺寸的,你也可以找到百分比值:
function passTeste(arg) {
const element = document.querySelector("#mainImage-teste-js > div")
const transformValue = window.getComputedStyle(element).transform;
const w = window.getComputedStyle(element).width;
var matrix = new WebKitCSSMatrix(transformValue);
console.log(w);
console.log(matrix.m41);
console.log(matrix.m41/parseInt(w)*100+"%");
}
.container {
width: 200px;
overflow: hidden;
}
.product__image--mainImage {
display: flex;
transition: 2s;
transform: translate(-200%);
}
.product__image--mainImage img {
max-width: 100%;
}
.container>p {
display: flex;
justify-content: space-between;
font-size: 45px;
cursor: pointer;
}
<div id="mainImage-teste-js" class="container">
<div class="product__image--mainImage">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTOues59dCMXRYVPt6H8XvWGAuPlwHBprFWEw&usqp=CAU" />
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSUEwJ_CKwAt4M1k_UTZSUCVUTy2XWjWLdK3w&usqp=CAU" />
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTOues59dCMXRYVPt6H8XvWGAuPlwHBprFWEw&usqp=CAU" />
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSUEwJ_CKwAt4M1k_UTZSUCVUTy2XWjWLdK3w&usqp=CAU" />
</div>
<p onclick="passTeste()">></p>
</div>
添加回答
举报