简体   繁体   English

使用javascript值进行CSS转换

[英]CSS transition with javascript values

I want to create a gallery site and wanted to have images that expand on hover (with transitions) and go full size when clicked on. 我想创建一个画廊网站,并希望在悬停时(使用过渡)扩展图像,并在单击时使用完整大小。 The hover expansion works fine with CSS only and the full-size expansion works with javascript. 悬停扩展仅适用于CSS,而全尺寸扩展适用于javascript。 But after the image was clicked once, the hover transition doesn't work anymore. 但是在单击图像一次后,悬停转换不再起作用。 I currently suspect that the value set in javascript just overwrites the CSS counterpart. 我目前怀疑在javascript中设置的值只会覆盖CSS对应物。 My question would be whether that is true at all and how one an work around it. 我的问题是,这是否真实,以及如何解决这个问题。

my code: 我的代码:

 var imgSelected = false; var imgElement; var fullSize = function(element) { imgSelected = true; imgElement = element; $(element).addClass("fullSize"); $(element).removeClass("images div:hover"); $(element).children("img").removeClass("img:hover"); $("body").css({"visibility":"hidden", "overflow":"hidden"}); $(element).css("visibility", "visible"); $(element).siblings("div").css("visibility", "hidden"); var imgW = $(element).children("img").width(); var imgH = $(element).children("img").height(); if (imgH/imgW * window.innerWidth > window.innerHeight) { $(".fullSize").css({"background-size":"auto 100%"}); $(".fullSize img").css({"top":"0", "height":"100%", "width":"auto", "left":"50%", "transform":"translate(-50%, 0)"}); //stop transition $(".fullSize img").css({"transition":"none", "-webkit-transition":"none", "-moz-transition":"none"}); } else if (imgW/imgH * window.innerHeight > window.innerWidth) { $(".fullSize").css({"background-size":"100% auto"}); $(".fullSize img").css({"left":"0", "width":"100%", "height":"auto", "top":"50%", "transform":"translate(0, -50%)"}); //stop transition $(".fullSize img").css({"transition":"none", "-webkit-transition":"none", "-moz-transition":"none"}); } $(".close").css("visibility", "visible"); } var fullSizeReverse = function() { imgSelected = false; $(imgElement).removeClass("fullSize"); $(imgElement).addClass("images div:hover"); $(imgElement).children("img").addClass("img:hover"); $("body").css({"visibility":"visible", "overflow-y":"scroll"}); $(".close").css("visibility", "hidden"); $(imgElement).siblings("div").css("visibility", "visible"); $(imgElement).children("img").css({"width":"30%", "transform":"translate(0, 0)"}); //$(imgElement).css({"background-size":"30% 100%"}); imgElement = null; setTimeout(function() {}, 100); } $(".images div").click(function(e) { fullSize(this); }); 
 /* gallery images */ .images { display: flex; flex-direction: column; justify-content: center; align-items: center; margin-bottom: 0; } .images div { pointer-events: none; display: flex; justify-content: center; background-size: 30% 100%; background-position: center top; background-repeat: no-repeat; -webkit-transition: background-size 0.8s ease-in-out; -moz-transition: background-size 0.8s ease-in-out; transition: background-size 0.8s ease-in-out; margin-bottom: 40vh; } .images div:last-child { margin-bottom: 15vh; } img { pointer-events: auto; width: 30%; height: auto; opacity: 0; -webkit-transition: width 0.8s ease-in-out, opacity 2s ease-in-out; -moz-transition: width 0.8s ease-in-out, opacity 2s ease-in-out; transition: width 0.8s ease-in-out, opacity 2s ease-in-out; } .images div:hover { background-size: 40% 100%; cursor: pointer; } img:hover { width: 40%; height: auto; opacity: 1; } .fullSize { position: fixed; z-index: 1; } .fullSize img { position: fixed; opacity: 1; cursor: "default"; z-index: 1; } /* close button */ .close { visibility: hidden; position: fixed; top: 2vmax; left: 2vmax; width: 32px; height: 32px; opacity: 0.3; z-index: 2; } .close:hover { opacity: 1; } .close:before, .close:after { position: absolute; left: 15px; content: ' '; height: 33px; width: 2px; background-color: #333; } .close:before { transform: rotate(45deg); } .close:after { transform: rotate(-45deg); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="images"> <h3>Images</h3> <a href="#" class="close" onclick="fullSizeReverse()"></a> <div id="work1"> <img src="../img/work1color.jpg"/> </div> <div id="work2"> <img src="../img/work2color.png"/> </div> <div id="work3"> <img src="../img/work3color.jpg"/> </div> </div> 

I have also recreated a simpler version of the effect with CodePen: 我还用CodePen重新创建了一个更简单的效果版本:

https://codepen.io/monodualist/pen/NBdvNw https://codepen.io/monodualist/pen/NBdvNw

here 这里

    $(imgElement).children("img").css({"width":"30%", "transform":"translate(0, 0)"});

should be 应该

    $(imgElement).children("img").css({"width":"", "transform":""});

in codepen, you have to unset the width and height like this 在codepen中,你必须像这样取消设置宽度和高度

var boxClicked = false;

function fullSize(element) {
  if (boxClicked) {
    boxClicked = false;
    element.style.width = null;
    element.style.height = null;    
  } else {
    boxClicked = true;
    element.style.width = "250px";
    element.style.height = "250px"; 
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM