javascript - 从元素中删除不需要的类
问题描述
我有一个图像滑块,当用户从左向右拖动时,它会显示图像之前和之后的图像。'draggable' 类被添加到这些图像中,但也添加到我的英雄图像中。我认为这些元素是自动生成的,但我找不到如何或在哪里(我没有构建这个,只是负责修复它!)。问题是当滑块被拖动时,英雄图像也会来回拖动。我无法弄清楚如何从我的英雄图像所在的元素中消除“可拖动”类。
// Call & init
$(document).ready(function () {
$(".ba-slider").each(function () {
var cur = $(this);
// Adjust the slider
var width = cur.width() + "px";
cur.find(".resize img").css("width", width);
// Bind dragging events
drags(cur.find(".handle"), cur.find(".resize"), cur);
});
});
// Update sliders on resize.
// Because we all do this: i.imgur.com/YkbaV.gif
$(window).resize(function () {
$(".ba-slider").each(function () {
var cur = $(this);
var width = cur.width() + "px";
cur.find(".resize img").css("width", width);
});
});
function drags(dragElement, resizeElement, container) {
// Initialize the dragging event on mousedown.
dragElement
.on("mousedown touchstart", function (e) {
dragElement.addClass("draggable");
resizeElement.addClass("resizable");
// Check if it's a mouse or touch event and pass along the correct value
var startX = e.pageX ? e.pageX : e.originalEvent.touches[0].pageX;
// Get the initial position
var dragWidth = dragElement.outerWidth(),
posX = dragElement.offset().left + dragWidth - startX,
containerOffset = container.offset().left,
containerWidth = container.outerWidth();
// Set limits
minLeft = containerOffset + 10;
maxLeft = containerOffset + containerWidth - dragWidth - 10;
// Calculate the dragging distance on mousemove.
dragElement
.parents()
.on("mousemove touchmove", function (e) {
// Check if it's a mouse or touch event and pass along the correct value
var moveX = e.pageX ? e.pageX : e.originalEvent.touches[0].pageX;
leftValue = moveX + posX - dragWidth;
// Prevent going off limits
if (leftValue < minLeft) {
leftValue = minLeft;
} else if (leftValue > maxLeft) {
leftValue = maxLeft;
}
// Translate the handle's left value to masked divs width.
widthValue =
((leftValue + dragWidth / 2 - containerOffset) * 100) /
containerWidth +
"%";
// Set the new values for the slider and the handle.
// Bind mouseup events to stop dragging.
$(".draggable")
.css("left", widthValue)
.on("mouseup touchend touchcancel", function () {
$(this).removeClass("draggable");
resizeElement.removeClass("resizable");
});
$(".resizable").css("width", widthValue);
})
.on("mouseup touchend touchcancel", function () {
dragElement.removeClass("draggable");
resizeElement.removeClass("resizable");
});
e.preventDefault();
})
.on("mouseup touchend touchcancel", function (e) {
dragElement.removeClass("draggable");
resizeElement.removeClass("resizable");
});
}
[![元素控制台]
这是生成的,但不是在源代码中。
<section class="intro-section">
<div class="frame">
<?php if( have_rows('intro_slide') ): ?>
<div class="intro-slider slick-slider">
<?php while( have_rows('intro_slide') ): the_row();
$title = get_sub_field('title') ? get_sub_field('title') : get_the_title();
$text = get_sub_field('text');
$image = get_sub_field('image');
$image2 = get_sub_field('logo');
$icon = get_sub_field('note_icon');
$number = get_sub_field('note_number');
$note = get_sub_field('note_text');
?>
<div class="slide bg-stretch"<?php if($image): ?> style="background-image: url(<?php echo $image['url']; ?>);"<?php endif; ?>>
<div class="container-wrap">
<?php if($image2): ?><div class="logo"><img src="<?php echo $image2['url']; ?>" width="227" alt="<?php bloginfo( 'name' ); ?>"></div><?php endif; ?>
<div class="text-overlay">
<?php if($title): ?><h1 class="h2"><?php echo $title; ?></h1><?php endif; ?>
<div class="text-block">
<?php echo $text; ?>
</div>
</div>
最初我尝试只定位元素并删除类,但我的变量出现未定义。
let x = document.getElementsByClassName('slick-list');
x.classList.remove('draggable');
我在这里有点茫然。任何建议将不胜感激!!谢谢!!
解决方案
假设 x 为您提供了必须删除类的元素。
let x = document.getElementsByClassName('slick-list');
x.classList.forEach(elem => {
elem.classList.remove('draggable');
});
推荐阅读
- firebase - 使用 .limitToFirst 或 .limitToLast 仍会返回 Firebase 实时数据库中的全部数据
- python - Tkinter 错误:_tkinter.TclError:未知选项“-menu”
- shell - 我的 cron 作业失败了,但是脚本执行得很好。有人可以帮忙吗?
- javascript - 如何从命令行或 javascript 打开 WhatsApp Mac
- r - 创建一个新的 var,其值应该是 r 中其他两个变量之间的平方差
- python - 从 2.7 升级到 3.6 时出现 python requests.post 错误
- ios - 在 Web 视图中从社交媒体下载视频
- python - 每次部署后强制重新安装 Python 包(Flask + Azure App Service)
- django - 应用程序无法读取 django 中的静态文件
- listview - 删除项目时列表不更新