Can toggleClass Work in Safari? (JQuery)

2019-03-06 12:10发布

问题:

I have yet to find a solution to an issue that I am facing with a script that uses .toggleClass() to toggle the visibility of an element. The example below currently adheres the .show class to #pgnav in Firefox and Chrome, but for some reason, the action does not apply in Safari at all.

Please view the snippet below for further reference:

$(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>

回答1:

On safari visibility doesn't work in some elements, a way to be able to realize is to save the elements, to modify them and to replace them, use detach in jquery and append to replace them.



回答2:

$(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>