Jquery / Add Class如果Viewport中有相同的数据属性值

flmtquvp  于 2024-01-07  发布在  jQuery
关注(0)|答案(1)|浏览(117)

加价:

  1. <div class="content" data-id="XX"></div>
  2. <div class="media" data-id="XX"></div>
  3. <div class="content" data-id="XY"></div>
  4. <div class="media" data-id="XY"></div>
  5. etc.

字符串
jquery:

  1. $.fn.isInViewport = function() {
  2. var elementTop = $(this).offset().top +600;
  3. var elementBottom = elementTop + $(this).outerHeight();
  4. var viewportTop = $(window).scrollTop();
  5. var viewportBottom = viewportTop + $(window).height();
  6. return elementBottom > viewportTop && elementTop < viewportBottom;
  7. };
  8. $(window).on('resize scroll', function() {
  9. dataid = $('.sticky-slider-content').data('id');
  10. if ($(dataid).isInViewport()) {
  11. $('.media').removeClass('active');
  12. $('.media[data-id="'+dataid+'"]').addClass('active');
  13. }
  14. });


这媒体ist设置为粘.当内容进入viewport我想给给予媒体与相同的数据-id一个类'活动'.
最初我想到了这个,但我想避免重复:

  1. $(window).on('resize scroll', function() {
  2. if ($('.content[data-id="3a241cb2"]').isInViewport()) {
  3. $('.media').removeClass('active');
  4. $('.media[data-id="3a241cb2"]').addClass('active');
  5. }
  6. if ($('.content[data-id="6a91e7bb"]').isInViewport()) {
  7. $('.media').removeClass('active');
  8. $('.media[data-id="6a91e7bb"]').addClass('active');
  9. }
  10. });

mzsu5hc0

mzsu5hc01#

我会坚持确定 *active的 * data-id值并查询相关的.media元素,然后使用JavaScript的IntersectionObserver API(如this related answer)来检测元素何时进入或退出视口

  1. const inViewport = (entries, observer) => {
  2. entries.forEach(entry => {
  3. // (This example is a proof of concept and might need optimization)
  4. const dataId = entry.target.dataset.id; // i.e: "3a241cb2"
  5. const elsMedia = document.querySelectorAll(`[data-id="${dataId }"]`);
  6. elMedia.classList.toggle("active", entry.isIntersecting);
  7. });
  8. };
  9. const Obs = new IntersectionObserver(inViewport);
  10. const obsOptions = {}; //See: https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Intersection_observer_options
  11. // Attach observer to every [data-inviewport] element:
  12. document.querySelectorAll('.content[data-id]').forEach(el => {
  13. Obs.observe(el, obsOptions);
  14. });

字符串
使用jQuery
以及使用inViewport micro jQuery plugin,其提供对指定元素集合的回调,其中参数px是每个 * 相交 *(视口内)元素的可见高度的整数值:

  1. /**
  2. * inViewport jQuery plugin by Roko C.B.
  3. * http://stackoverflow.com/a/26831113/383904
  4. * Returns a callback function with an argument holding
  5. * the current amount of px an element is visible in viewport
  6. * (The min returned value is 0 (element outside of viewport)
  7. */
  8. ;(function($, win) {
  9. $.fn.inViewport = function(cb) {
  10. return this.each(function(i,el) {
  11. function visPx(){
  12. var elH = $(el).outerHeight(),
  13. H = $(win).height(),
  14. r = el.getBoundingClientRect(), t=r.top, b=r.bottom;
  15. return cb.call(el, Math.max(0, t>0? Math.min(elH, H-t) : Math.min(b, H)));
  16. }
  17. visPx();
  18. $(win).on("resize scroll", visPx);
  19. });
  20. };
  21. }(jQuery, window));
  22. // Use like:
  23. $(".content[data-id]").inViewport(function(px) {
  24. // `px` represents the amount of visible height
  25. if (px > 0) {
  26. // Is in viewport logic goes here
  27. const dataId = $(this).data("id"); // i.e: "3a241cb2"
  28. $(".media").removeClass("active");
  29. $(`.media[data-id="${dataId}"]`).addClass("active");
  30. } else {
  31. // Not in viewport logic goes here
  32. }
  33. });

展开查看全部

相关问题