addClass()/ removeClass()转换导致Safari中的分秒位置跳转?

时间:2018-03-03 00:45:31

标签: javascript jquery lightbox

什么会导致addClass() / removeClass()属性打破流畅的过渡?

以下是我正在使用的当前脚本:

$(document).ready(function() {
  var o = 0;
  $("a.lbimg > img.lb-click").click(function() {
    var s = $("body");
    o = $(window).scrollTop(), s.css("position", "fixed"), s.css("top", "-" + o + "px"), s.css("background-position", "0 -" + o + "px"), s.addClass("no-scroll"), $("#lb-view").addClass("target");
  }), $("#lb-controls").on("click", function() {
    var s = $("body");
    s.css("position", ""), s.css("background-position", ""), o = $(window).scrollTop(o), s.removeClass("no-scroll"), $("#lb-view").removeClass("target");
  });
});

有点难以解释;但是,这似乎是特定于Safari的问题。

这是主要问题:点击照片后,它会在灯箱窗口中打开图像。然后,在关闭窗口时,body位置仅在一瞬间跳转/移位,然后再调整回当前窗口滚动位置。从本质上讲,它打破了功能的可见流动性。

以下是我正在使用的CSS类型的片段:

body,
html {
  height: 100%;
  width: 100%;
  background-size: cover;
  background-repeat: no-repeat;
}

body {
  margin: 0;
  padding: 0;
  border: 0;
  outline: 0;
  z-index: -7;
}

body.no-scroll {
  overflow: hidden;
  width: 100%;
  height: auto;
}

img {
  z-index: 1;
}

a.lbimg {
  cursor: pointer;
  display: block;
  line-height: 0;
}

a.lbimg img {
  margin: 0 auto;
  padding: 0;
  border: 0;
}

.lb-click {
  height: auto;
}

.customlightbox {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  opacity: 0;
  position: fixed;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  background: rgba(0, 0, 0, 0.7);
  z-index: -8;
}

.lbimg-wrap {
  width: 100%;
  height: 100%;
  position: relative;
  padding: 45px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-align: center;
}

.customlightbox img {
  position: relative;
  width: auto;
  margin: auto;
  max-width: 100%;
  max-height: 100%;
  top: 50%;
  opacity: 0;
  -webkit-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}

#lb-controls {
  position: fixed;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  height: 50px;
  width: 50px;
  top: -50px;
  opacity: .7;
  cursor: pointer;
  z-index: 5;
}

#lb-close {
  display: block;
  position: absolute;
  overflow: hidden;
  height: 30px;
  width: 30px;
  right: 10px;
  top: 10px;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}

#lb-close:before,
#lb-close:after {
  content: '';
  display: block;
  position: absolute;
  background: white;
  -webkit-border-radius: 2px;
  -moz-border-radius: 2px;
  border-radius: 2px;
}

#lb-close:before {
  width: 2px;
  height: 0;
  top: 0;
  left: 14px;
}

#lb-close:after {
  width: 0;
  height: 2px;
  top: 14px;
  left: 0;
}

.customlightbox.target {
  display: inline-block;
  z-index: 4;
}

.customlightbox.target,
.customlightbox.target img {
  opacity: 1;
}

.customlightbox.target~#lb-controls {
  top: -3px;
}

.customlightbox.target~#lb-controls #lb-close:after {
  width: 30px;
}

.customlightbox.target~#lb-controls #lb-close:before {
  height: 30px;
}

.lb-animate {
  -webkit-transition: 0.5s ease-in-out;
  -o-transition: 0.5s ease-in-out;
  transition: 0.5s ease-in-out;
}

关于此的任何输入?

1 个答案:

答案 0 :(得分:0)

我在这里标记了一个类似问题的答案:https://stackoverflow.com/a/49101640/9214076。此外,我已应用此答案,并在此处更新了原始问题:How to Make a Fixed Element Stay Relative to the Current Top Scroll Position?