gpt4 book ai didi

javascript - 在刷新页面时记住类名

转载 作者:行者123 更新时间:2023-11-28 13:53:57 26 4
gpt4 key购买 nike

我已经尝试了几种不同的方法来做到这一点,但都做不好。

每当 div 进入视口(viewport)时,滚动脚本都会添加一个“.prod__viewed”类,所以我想让浏览器记住哪些 div 在重新加载页面时具有“.prod__viewed”,我已经尝试过 session 存储空间,但还不够。

$(window).on('resize load scroll', function() {
$('.prod__item').each(function() {
if ($(this).prodInViewport()) {
$(this).addClass('prod__viewed');
}
});

var numItems = $('.prod__item').length;
var totalNumItems = $('.prod__viewed').length;
var prodCountCheck = $('.prod__count__container');
var positionY = $(this).scrollTop();
var pageHeight = $(this).height();
var scrollHeight = $('.prod__wrapper').height();
var scrollPercent = (positionY / (scrollHeight - pageHeight)) * 100;
var prodCountElement = $('.prod__count__container').outerHeight();

if (prodCountCheck.length == 1) {
$('.prod__count__content')
.html('<span>You&#39;ve seen <strong>' + totalNumItems + '</strong> of <strong>' + numItems + '</strong> prods</span>');

$('.prod__load__bar').css({
'top': prodCountElement,
'width': scrollPercent + "%"
});
}
});

$.fn.prodInViewport = function() {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();
var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();
return elementBottom > viewportTop && elementTop < viewportBottom;
};

sessionStorage.setItem("prodViewed",
$(".prod__item").addClass("prod__viewed"));


if (sessionStorage.getItem("prodViewed")) {
$(".prod__item").addClass(sessionStorage.getItem("prodViewed"));
}
html {
box-sizing: border-box;
font-size: 62.5%;
-webkit-overflow-scrolling: touch;
}

*,
*:before,
*:after {
box-sizing: inherit;
}

body {
background: #fff;
color: #333;
font-family: "Montserrat", sans-serif;
font-size: 1.6rem;
line-height: 1.5;
margin: 0;
}

a {
color: #333;
text-decoration: none
}

.header__content {
font-size: 18px;
line-height: 1.8;
margin-left: auto;
margin-right: auto;
max-width: 500px;
width: 100%;
}

.prod__wrapper {
margin-left: auto;
margin-right: auto;
margin-top: 60px;
max-width: 500px;
width: 100%;
}

.prod__container {
display: flex;
flex-wrap: wrap;
margin-left: -20px;
margin-top: -60px;
padding-left: 20px;
padding-right: 20px;
}

.prod__item {
flex-basis: 50%;
max-width: 50%;
padding-left: 20px;
padding-top: 60px;
}

.prod__item--img {
height: 0;
padding-bottom: 100%;
position: relative;
}

.prod__item--img img {
left: 50%;
max-height: 100%;
max-width: 100%;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
width: auto;
}

.prod__name {
font-weight: 700;
line-height: 1.2;
margin-top: 10px;
}

.prod__price {
margin-top: 10px;
}

.prod__count__wrapper {
margin-left: auto;
margin-right: auto;
margin-top: 40px;
max-width: 500px;
position: sticky;
top: 0;
z-index: 1;
}

.prod__count__container {
align-items: center;
background: darkblue;
color: white;
display: flex;
font-size: 20px;
height: 60px;
padding-left: 20px;
padding-right: 20px;
justify-content: center;
text-align: center;
width: 100%;
}

.prod__count__content {
position: relative;
top: -1px;
}

.prod__load__bar {
background: dodgerblue;
height: 6px;
left: 0;
max-width: 100%;
position: absolute;
top: 0;
}

.random__content {
padding-left: 20px;
padding-right: 20px;
margin-left: auto;
margin-right: auto;
margin-top: 60px;
max-width: 500px;
width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<section class="prod__wrapper">
<div class="prod__container">
<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div>
<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div>
<div class="prod__price">
£14.99
</div>
</div>
</div>
<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div>
<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div>
<div class="prod__price">
£14.99
</div>
</div>
</div>
<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div>
<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div>
<div class="prod__price">
£14.99
</div>
</div>
</div>
</div>
</section>

我这里有一个可用的 CodePen:https://codepen.io/nickelse/pen/vYYoVZR

最佳答案

我想我已经按照您的意愿工作了。在滚动函数中,我将 View 中元素的索引添加到一个数组中,并将该数组保存到 session 存储中。然后,当页面加载时,我获取已查看对象的索引数组,并将类添加到该元素。

这是一个有效的 fiddle ,因为出于安全原因,堆栈溢出不允许您访问 session 存储。 https://jsfiddle.net/9dftzw8m/1/

$.fn.prodInViewport = function() {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();
var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();
return elementBottom > viewportTop && elementTop < viewportBottom;
};

Array.prototype.pushUnique = function (item){
if(this.indexOf(item) == -1) {
this.push(item);
return true;
}
return false;
}


$(window).on('resize load scroll', function() {
$('.prod__item').each(function() {
if ($(this).prodInViewport()) {
$(this).addClass('prod__viewed');

let prodsViewed = JSON.parse( sessionStorage.getItem("prodViewed") ) || [];
prodsViewed.pushUnique( $(this).index() );
sessionStorage.setItem("prodViewed", JSON.stringify( prodsViewed ) );
}
});

var numItems = $('.prod__item').length;
var totalNumItems = $('.prod__item.prod__viewed').length;
var prodCountCheck = $('.prod__count__container');
var positionY = $(this).scrollTop();
var pageHeight = $(this).height();
var scrollHeight = $('.prod__wrapper').height();
var scrollPercent = (positionY / (scrollHeight - pageHeight)) * 100;
var prodCountElement = $('.prod__count__container').outerHeight();

if (prodCountCheck.length == 1) {
$('.prod__count__content')
.html('<span>You&#39;ve seen <strong>'+totalNumItems+'</strong> of <strong>'+numItems+'</strong> prods</span>');

$('.prod__load__bar').css({
'top': prodCountElement,
'width': scrollPercent+"%"
});
}
});


const viewedItemsIndexList = JSON.parse( sessionStorage.getItem("prodViewed") );
if( viewedItemsIndexList ) {
for ( let i = 0; i < viewedItemsIndexList.length; i++) {
$( ".prod__item" ).eq(viewedItemsIndexList[i]).addClass("prod__viewed");
}
}
html {
box-sizing: border-box;
font-size: 62.5%;
-webkit-overflow-scrolling: touch;
}

*, *:before, *:after { box-sizing: inherit; }

body {
background: #fff;
color: #333;
font-family: "Montserrat", sans-serif;
font-size: 1.6rem;
line-height: 1.5;
margin: 0;
}

a {
color: #333;
text-decoration: none
}


.header__content {
font-size: 18px;
line-height: 1.8;
margin-left: auto;
margin-right: auto;
max-width: 500px;
width: 100%;
}


.prod__wrapper {
margin-left: auto;
margin-right: auto;
margin-top: 60px;
max-width: 500px;
width: 100%;
}

.prod__container {
display: flex;
flex-wrap: wrap;
margin-left: -20px;
margin-top: -60px;
padding-left: 20px;
padding-right: 20px;
}

.prod__item {
flex-basis: 50%;
max-width: 50%;
padding-left: 20px;
padding-top: 60px;
}

.prod__item--img {
height: 0;
padding-bottom: 100%;
position: relative;
}

.prod__item--img img {
left: 50%;
max-height: 100%;
max-width: 100%;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
width: auto;
}

.prod__name {
font-weight: 700;
line-height: 1.2;
margin-top: 10px;
}

.prod__price { margin-top: 10px; }

.prod__count__wrapper {
margin-left: auto;
margin-right: auto;
margin-top: 40px;
max-width: 500px;
position: sticky;
top: 0;
z-index: 1;
}

.prod__count__container {
align-items: center;
background: darkblue;
color: white;
display: flex;
font-size: 20px;
height: 60px;
padding-left: 20px;
padding-right: 20px;
justify-content: center;
text-align: center;
width: 100%;
}

.prod__count__content {
position: relative;
top: -1px;
}

.prod__load__bar {
background: dodgerblue;
height: 6px;
left: 0;
max-width: 100%;
position: absolute;
top: 0;
}


.random__content {
padding-left: 20px;
padding-right: 20px;
margin-left: auto;
margin-right: auto;
margin-top: 60px;
max-width: 500px;
width: 100%;
}

/* .prod__viewed {background: red;} */
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section class="header__content">
<h1>Counting Them Divs, Yo!</h1>

<p>This script will count up any div that has been seen inside of the viewport and give a handy little count so you don't lose track, also added a sweet loader bar just in case :D</p>

<p><strong>Give it a scroll!</strong></p>
</section>


<section class="prod__count__wrapper">
<div class="prod__count__container">
<div class="prod__count__content"></div><!-- /.prod__count__content -->
</div><!-- /.prod__count__container -->

<div class="prod__load__bar"></div><!-- /.prod__load__bar -->
</section><!-- /.prod__count__wrapper -->


<section class="prod__wrapper">
<div class="prod__container">
<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->

<div class="prod__item">
<div class="prod__item--img">
<img src="https://images-na.ssl-images-amazon.com/images/I/81Lz-p7eRDL._SL1500_.jpg">
</div><!-- /.prod__item--img -->

<div class="prod__item--deets">
<div class="prod__name">
My Hero Academia Two Heroes Blu Ray
</div><!-- /.prod__name -->

<div class="prod__price">
£14.99
</div><!-- /.prod__price -->
</div><!-- /.prod__item--deets -->
</div><!-- /.prod__item -->
</div><!-- /.prod__container -->
</section><!-- /.prod__wrapper -->



<section class="random__content">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.</p>

<p>Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.</p>

<p>Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus.</p>

<p>Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante.</p>

<p>Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus.</p>

<p>Nullam accumsan lorem in dui. Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia. Nam pretium turpis et arcu. Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Sed aliquam ultrices mauris.</p>

<p>Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. Vestibulum volutpat pretium libero. Cras id dui. Aenean ut eros et nisl sagittis vestibulum. Nullam nulla eros, ultricies sit amet, nonummy id, imperdiet feugiat, pede. Sed lectus.</p>

<p>Donec mollis hendrerit risus. Phasellus nec sem in justo pellentesque facilisis. Etiam imperdiet imperdiet orci. Nunc nec neque. Phasellus leo dolor, tempus non, auctor et, hendrerit quis, nisi. Curabitur ligula sapien, tincidunt non, euismod vitae, posuere imperdiet, leo. Maecenas malesuada. Praesent congue erat at massa. Sed cursus turpis vitae tortor. Donec posuere vulputate arcu. Phasellus accumsan cursus velit. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed aliquam, nisi quis porttitor congue, elit erat euismod orci, ac</p>
</section><!-- /.random__content -->

关于javascript - 在刷新页面时记住类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59107541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com