gpt4 book ai didi

javascript - Jquery 将类添加到设备上的元素

转载 作者:太空宇宙 更新时间:2023-11-04 01:47:56 25 4
gpt4 key购买 nike

我正在构建一个响应式 slider ,它将具有两种不同的行为。

一个用于桌面。一个用于移动设备。

步骤是:

  • 检查设备模式是否处于事件状态并添加类移动
  • 检查桌面模式是否处于事件状态并添加类Desktop

我在代码方面遇到的问题是:

  • 如何向 <li> 添加一个事件类点击?
  • 如何删除激活到 <li> 的类如果我点击 close-btn

    Demo Here

//Check if device mode is active and add class Mobile 
mobileViewUpdate();
$(window).on('load, resize', mobileViewUpdate);

function mobileViewUpdate() {
var viewportWidth = $(window).width();
if (viewportWidth < 600) {
$(".items").addClass("mobile");
$(".mobile").removeClass("desktop");
} else {

$(".items").addClass("desktop");
$(".mobile").removeClass("mobile");
$(".mobile").removeClass("active");
}
};
// Now add class active to li if parent has class Mobile
$(".mobile li").click(function(e) {
e.preventDefault;
// remove classes from all

if (!$(this).hasClass("active")) {
$(this).addClass("active");
}
});

$(".close-btn").click(function(e) {
e.preventDefault;
$(this).find('li.active').removeClass('active')
});
.items {
position: fixed;
top: 50%;
left: 50%;
text-align: center;
width: 5000px;
-webkit-transform: translateY(-50%) translateX(-50%);
transform: translateY(-50%) translateX(-50%);
}

.items li {
position: relative;
vertical-align: middle;
display: inline-block;
list-style: none;
width: 320px;
height: 320px;
/*background-color: rgba(228, 0, 59, 1);*/
-webkit-transition-duration: .5s;
transition-duration: .5s;
overflow: hidden;
cursor: pointer;
}

.items li .bg-img {
position: absolute;
width: 100%;
height: 100%;
background-size: cover;
background-position: center top;
}

.items li>a {
color: white;
text-decoration: none;
cursor: pointer;
width: 100%;
height: 100%;
display: block;
position: relative;
z-index: 2;
}

.items li>a .content {
/* background: -webkit-linear-gradient(transparent, rgba(228, 0, 59, 0.75));
background: linear-gradient(transparent, rgba(228, 0, 59, 0.75));*/
width: 100%;
height: 100%;
position: absolute;
bottom: 0;
left: 50%;
-webkit-transform: translateY(100%) translateX(-50%);
transform: translateY(100%) translateX(-50%);
-webkit-transition-duration: 0.5s;
transition-duration: 0.5s;
opacity: 0;
padding: 10px 10px 10px 10px;
}

.items li>a .content h2 {
font-weight: 300;
color: white;
font-size: 30px;
margin: 0;
padding: 0;
text-align: center;
}


/*make bg on MOBILE personalized*/

.mobile {
border: 1px solid red;
}

.mobile li.active a .content {
-webkit-transform: translateY(0) translateX(-50%);
transform: translateY(0) translateX(-50%);
opacity: 1;
}

.mobile li .content>span.close-btn {
position: absolute;
top: 10px;
right: -10px;
-webkit-mask: url(https://raw.githubusercontent.com/encharm/Font-Awesome-SVG-PNG/master/black/svg/close.svg) no-repeat;
mask: url(https://raw.githubusercontent.com/encharm/Font-Awesome-SVG-PNG/master/black/svg/close.svg) no-repeat;
-webkit-mask-size: 20px;
mask-size: 20px;
background-color: #fff;
cursor: pointer;
display: block;
text-indent: -9999em;
z-index: 3;
}


/*make bg on DESKTOP personalized*/

.desktop {
border: 1px solid blue;
}

.desktop li .content>span.close-btn {
display: none;
}

.desktop li:hover {
-webkit-transition-delay: .5s;
transition-delay: .5s;
width: calc(320px + 40px);
height: calc(320px + 40px);
}

.desktop li:hover a .content {
-webkit-transform: translateY(0) translateX(-50%);
transform: translateY(0) translateX(-50%);
-webkit-transition-delay: .75s;
transition-delay: .75s;
opacity: 1;
}
<div class="clearfix flex-auto py3">
<ul class="items">
<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>
</ul>

</div>

最佳答案

它不起作用,因为

1.(这是可选的)我认为您的点击功能应该包含在 mobileViewUpdate 功能中

2.close-btn.mobile li 的子项(在里面),所以当你点击它时,你也点击了 li 所以你可以同时删除和添加类

你应该检查(当点击移动 li 时)你点击的元素是否是关闭的 btn ,如果是,取消点击。因此,当您单击 close-btn 时,代码不会解释为您也单击了 li

3.在 close-btn 点击函数中编写 $(this).find('li.active').removeClass('active') 。 find() 仅在元素内部搜索。在这种情况下,li 是 close-btn 的父级,但不是直接的,因此您需要使用 parents() 来查找 li。事件

请参阅下面的代码或 jsfiddle jsFiddle

//Check if device mode is active and add class Mobile 
mobileViewUpdate();
$(window).on('load, resize', mobileViewUpdate);

function mobileViewUpdate() {
var viewportWidth = $(window).width();
if (viewportWidth < 600) {
$(".items").addClass("mobile");
$(".mobile").removeClass("desktop");
} else {

$(".items").addClass("desktop");
$(".mobile").removeClass("mobile");
$(".mobile").removeClass("active");
}
};
// Now add class active to li if parent has class Mobile
$(".mobile li").click(function(e) {
e.preventDefault;
// remove classes from all
if ($(e.target).is('.close-btn')) {

return;
}

if (!$(this).hasClass("active")) {

$(this).addClass("active");
}
$("li.active").not(this).removeClass("active")
});

$(".close-btn").click(function(e) {

$(this).parents('li').removeClass("active")
});
.items {
position: fixed;
top: 50%;
left: 50%;
text-align: center;
width: 5000px;
-webkit-transform: translateY(-50%) translateX(-50%);
transform: translateY(-50%) translateX(-50%);
}

.items li {
position: relative;
vertical-align: middle;
display: inline-block;
list-style: none;
width: 320px;
height: 320px;
/*background-color: rgba(228, 0, 59, 1);*/
-webkit-transition-duration: .5s;
transition-duration: .5s;
overflow: hidden;
cursor: pointer;
}

.items li .bg-img {
position: absolute;
width: 100%;
height: 100%;
background-size: cover;
background-position: center top;
}

.items li >a {
color: white;
text-decoration: none;
cursor: pointer;
width: 100%;
height: 100%;
display: block;
position: relative;
z-index: 2;
}

.items li > a .content {
/* background: -webkit-linear-gradient(transparent, rgba(228, 0, 59, 0.75));
background: linear-gradient(transparent, rgba(228, 0, 59, 0.75));*/
width: 100%;
height: 100%;
position: absolute;
bottom: 0;
left: 50%;
-webkit-transform: translateY(100%) translateX(-50%);
transform: translateY(100%) translateX(-50%);
-webkit-transition-duration: 0.5s;
transition-duration: 0.5s;
opacity: 0;
padding: 10px 10px 10px 10px;
}

.items li >a .content h2 {
font-weight: 300;
color: white;
font-size: 30px;
margin: 0;
padding: 0;
text-align: center;
}


/*make bg on MOBILE personalized*/

.mobile {
border: 1px solid red;
}

.mobile li.active a .content {
-webkit-transform: translateY(0) translateX(-50%);
transform: translateY(0) translateX(-50%);
opacity: 1;
}

.mobile li .content >span.close-btn {
position: absolute;
top: 10px;
right: -10px;
-webkit-mask-size: 20px;
mask-size: 20px;
background-color: #fff;
cursor: pointer;
display: block;
text-indent: -9999em;
z-index: 9999;
height: 40px;
width: 40px;
background: Red;
}


/*make bg on DESKTOP personalized*/

.desktop {
border: 1px solid blue;
}

.desktop li .content >span.close-btn {
display: none;
}

.desktop li:hover {
-webkit-transition-delay: .5s;
transition-delay: .5s;
width: calc(320px + 40px);
height: calc(320px + 40px);
}

.desktop li:hover a .content {
-webkit-transform: translateY(0) translateX(-50%);
transform: translateY(0) translateX(-50%);
-webkit-transition-delay: .75s;
transition-delay: .75s;
opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="clearfix flex-auto py3">
<ul class="items">
<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>

<li>
<div class="bg-img" style="background-image: url('http://placehold.it/320x320');"></div>
<a href="#">
<div class="content">
<span class="close-btn">close btn</span>
<h2>Slider item</h2>
</div>
</a>
</li>
</ul>

</div>

关于javascript - Jquery 将类添加到设备上的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44108567/

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