toggleClass可以在Safari中使用吗? (jQuery的)

时间:2018-06-20 21:50:19

标签: javascript jquery safari visibility

我尚未找到使用.toggleClass()来切换元素可见性的脚本所面临的问题的解决方案。下面的示例当前在Firefox和Chrome中将.show类坚持为#pgnav,但是由于某些原因,该操作根本不适用于Safari。

请查看下面的代码片段以供进一步参考:

$(document).ready(function() {
  var $window = $(window);
  var div2 = $('#pgnav');
  var div1 = $('#container2');
  $window.on('scroll', function() {
    var scrollTop = document.documentElement.scrollTop;
    var viewport_height = $window.height();
    var scrollTop_bottom = scrollTop + viewport_height;
    var window_top_to_div2 = ($window.height() - div2.height()) / 2;
    var div1_top = div1.offset().top;
    var div1_height = div1.height();
    var div1_bottom = div1_top + div1_height;
    if (scrollTop >= (div1_top - window_top_to_div2) && (scrollTop + window.innerHeight) <= (div1_bottom + window_top_to_div2)) {
      div2.addClass('show')
    } else {
      div2.removeClass('show');
    }
  });
});
#pgnav {
  height: 50px;
  text-align: center;
  margin: auto;
  top: 0;
  bottom: 0;
  right: 50%;
  left: 50%;
  visibility: hidden;
  opacity: 0;
  -webkit-transition: opacity 500ms, visibility 500ms;
  -o-transition: opacity 500ms, visibility 500ms;
  transition: opacity 500ms, visibility 500ms;
  z-index: 1;
  position: fixed;
  max-width: 1000px;
  width: 100%;
  -webkit-transform: translateX(-50%);
  -ms-transform: translateX(-50%);
  transform: translateX(-50%);
}

#pgnav.show {
  visibility: visible;
  opacity: 1;
}

#pgnav .wrap {
  position: relative;
  height: 50px;
  width: 80%;
  margin: 0 auto;
}

#pgnav .navbtns,
#pgnav-min .navbtns {
  display: table-cell;
  width: 50px;
  height: 50px;
  position: absolute;
  top: 0;
  background: transparent;
}

#pgnav .navbtns svg,
#pgnav-min .navbtns svg {
  fill: blue;
  opacity: .8;
  overflow: visible;
  will-change: opacity;
  -webkit-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}

#pgnav .navbtns svg:hover,
#pgnav-min .navbtns svg:hover {
  opacity: 1;
}

#pgnav .prev {
  right: 0;
  margin-right: -25px;
}

#pgnav .next {
  left: 0;
  margin-left: -25px;
}

#container1,
#container3 {
  width: 60%;
  height: 1000px;
  background: yellow;
  margin: 0 auto;
  display: block;
  text-align: center;
}

#container2 {
  width: 60%;
  height: 2000px;
  margin: 0 auto;
  position: relative;
  background: blue;
  display: block;
  text-align: center;
  color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pgnav">
  <div class="wrap">
    <a href="#" class="navbtns prev" title="Go to Next Page">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50"><path d="M25,50A25,25,0,1,0,0,25,25,25,0,0,0,25,50ZM25,2A23,23,0,1,1,2,25,23,23,0,0,1,25,2Zm-3,9V39L32,25Z" style="fill-rule:evenodd"/></svg>
    </a>
    <a href="#" class="navbtns next" title="Go to Previous Page">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50"><path d="M25,50A25,25,0,1,1,50,25,25,25,0,0,1,25,50ZM25,2A23,23,0,1,0,48,25,23,23,0,0,0,25,2Zm3,9V39L18,25Z" style="fill-rule:evenodd"/></svg>
    </a>
  </div>
</div>
<div id="container1">
  Scroll down to <b>#container2</b>
</div>
<div id="container2">
  This is <b>#container2</b>
</div>
<div id="container3">
  Scroll up to <b>#container2</b>
</div>

2 个答案:

答案 0 :(得分:0)

在野生动物园中,可见性在某些元素中不起作用,一种能够实现的方法是保存元素,对其进行修改并替换它们,在jQuery中使用detach并附加以替换它们。

答案 1 :(得分:0)

$(document).ready(function() {
  var $window = $(window);
  var div2 = $('#pgnav');
  var div1 = $('#container2');
  $window.on('scroll', function() {
    var scrollTop = document.documentElement.scrollTop;
    var viewport_height = $window.height();
    var scrollTop_bottom = scrollTop + viewport_height;
    var window_top_to_div2 = ($window.height() - div2.height()) / 2;
    var div1_top = div1.offset().top;
    var div1_height = div1.height();
    var div1_bottom = div1_top + div1_height;
    if (scrollTop >= (div1_top - window_top_to_div2) && (scrollTop + window.innerHeight) <= (div1_bottom + window_top_to_div2)) {
      $('#main').prepend(div2);
    } else {
      $('#pgnav').detach();
      }
  });
});
#pgnav {
  height: 50px;
  text-align: center;
  margin: auto;
  top: 0;
  bottom: 0;
  right: 50%;
  left: 50%;
  visibility: visible;
  opacity: 1;
  -webkit-transition: opacity 500ms, visibility 500ms;
  -o-transition: opacity 500ms, visibility 500ms;
  transition: opacity 500ms, visibility 500ms;
  z-index: 1;
  position: fixed;
  max-width: 1000px;
  width: 100%;
  -webkit-transform: translateX(-50%);
  -ms-transform: translateX(-50%);
  transform: translateX(-50%);
}

#pgnav.show {
  visibility: visible;
  opacity: 1;
}

#pgnav .wrap {
  position: relative;
  height: 50px;
  width: 80%;
  margin: 0 auto;
}

#pgnav .navbtns,
#pgnav-min .navbtns {
  display: table-cell;
  width: 50px;
  height: 50px;
  position: absolute;
  top: 0;
  background: transparent;
}

#pgnav .navbtns svg,
#pgnav-min .navbtns svg {
  fill: blue;
  opacity: .8;
  overflow: visible;
  will-change: opacity;
  -webkit-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}

#pgnav .navbtns svg:hover,
#pgnav-min .navbtns svg:hover {
  opacity: 1;
}

#pgnav .prev {
  right: 0;
  margin-right: -25px;
}

#pgnav .next {
  left: 0;
  margin-left: -25px;
}

#container1,
#container3 {
  width: 60%;
  height: 1000px;
  background: yellow;
  margin: 0 auto;
  display: block;
  text-align: center;
}

#container2 {
  width: 60%;
  height: 2000px;
  margin: 0 auto;
  position: relative;
  background: blue;
  display: block;
  text-align: center;
  color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
<div id="pgnav">
  <div class="wrap">
    <a href="#" class="navbtns prev" title="Go to Next Page">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50"><path d="M25,50A25,25,0,1,0,0,25,25,25,0,0,0,25,50ZM25,2A23,23,0,1,1,2,25,23,23,0,0,1,25,2Zm-3,9V39L32,25Z" style="fill-rule:evenodd"/></svg>
    </a>
    <a href="#" class="navbtns next" title="Go to Previous Page">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50"><path d="M25,50A25,25,0,1,1,50,25,25,25,0,0,1,25,50ZM25,2A23,23,0,1,0,48,25,23,23,0,0,0,25,2Zm3,9V39L18,25Z" style="fill-rule:evenodd"/></svg>
    </a>
  </div>
</div>
<div id="container1">
  Scroll down to <b>#container2</b>
</div>
<div id="container2">
  This is <b>#container2</b>
</div>
<div id="container3">
  Scroll up to <b>#container2</b>
</div>
</div>