gpt4 book ai didi

javascript - JQuery 事件仅触发一次

转载 作者:行者123 更新时间:2023-11-29 16:30:49 27 4
gpt4 key购买 nike

我正在尝试使用SlimSelect创建一个多选下拉框每个字段中都有水平文本滚动。现在一切似乎都有效,但事实是,它只能工作一次(第一次单击某个值)。之后,mouseovermouseout 似乎就放弃了。我已经浏览过堆栈,但所有这些问题都有代码答案,并且缺乏对“为什么它只触发一次”的一般解释,所以这对我没有真正的帮助。

$('.ms-class > option').addClass('scroll-text ellipsis');

new SlimSelect({
select: '#slim-multi-select',
allowDeselectOption: true,
showContent: 'down',
})

$(".ss-option.scroll-text").on({
mouseover: function() {
$(this).removeClass("ellipsis");
var maxscroll = $(this)[0].scrollWidth;
var speed = maxscroll * 15;
$(this).animate({
scrollLeft: maxscroll
}, speed, "linear");
},
mouseout: function() {
$(this).stop();
$(this).addClass("ellipsis");
$(this).animate({
scrollLeft: 0
}, 'slow');
}
});
.ms-class {
width: 200px !important;
}

.ms-class .ss-content {
z-index: 2;
}

.ms-class .ss-content .ss-list {
max-height: 100% !important;
}

.ms-class .ss-multi-selected .ss-values .ss-value {
font-size: 14px;
margin: 5px 5px 5px 0;
background-color: #884fa1;
}

.ms-class .ss-content .ss-list .ss-option,
.ms-class .ss-content .ss-list .ss-option-selected {
display: inline-block;
width: 33%;
}

.ms-class .ss-content .ss-list .ss-option.ss-hide,
.ms-class .ss-content .ss-list .ss-option.ss-hide {
display: none;
}

.ms-class .ss-content .ss-list .ss-option,
.ms-class .ss-content .ss-list .ss-option-selected {
display: inline-block;
width: 25%;
}

.ms-class .ss-content .ss-list .ss-option {
vertical-align: middle;
text-align: center;
height: 50px;
line-height: 0.9em;
white-space: nowrap;
overflow: hidden !important;
}

.ms-class .ss-content .ss-list .ss-option:after {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}

.ss-option:hover {
background-color: #884fa1 !important;
color: white;
}

.ms-class .ss-multi-selected .ss-values .ss-disabled,
.ms-class .ss-content .ss-search input {
font-family: 'Exo 2', 'Font Awesome 5 Free';
font-weight: 900 !important;
color: #444444 !important;
}

.ms-class .ss-multi-selected .ss-values .ss-disabled {
margin: 0 8px;
}

.ms-class .ss-content .ss-list .ss-option:only-child,
.ms-class .ss-content .ss-list .ss-option.ss-disabled:only-child {
width: 100% !important;
}

.ms-class .ss-content .ss-list .ss-option:first-child:nth-last-child(2),
.ms-class .ss-content .ss-list .ss-option:first-child:nth-last-child(2)~.ss-option {
width: 50% !important;
}

.ms-class .ss-content .ss-list .ss-option:first-child:nth-last-child(3),
.ms-class .ss-content .ss-list .ss-option:first-child:nth-last-child(3)~.ss-option {
width: 33% !important;
}

.ms-class .ss-content .ss-list .ss-option.ss-disabled:only-child {
width: 100% !important;
}

.ms-class .ss-multi-selected {
border-radius: 5px !important;
border: solid 1px #e8e8e8 !important;
min-height: 42px !important;
line-height: 100%;
-webkit-tap-highlight-color: transparent;
}

.ms-class .ss-multi-selected .ss-add {
display: flex;
flex: 0 1 3px;
margin: 14px 12px 0 5px;
}

.ms-class .ss-multi-selected .ss-add .ss-plus,
.ms-class .ss-multi-selected .ss-add .ss-plus:after {
background: #999;
}

.ms-class .ss-content .ss-list .ss-option:not(.ss-disabled).ss-option-selected {
background-color: rgba(136, 79, 161, 0.32);
}

.ms-class .ss-content .ss-list .ss-option:not(.ss-disabled).ss-option-selected:hover {
color: white !important;
}

.ellipsis {
text-overflow: ellipsis;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/slim-select/1.23.0/slimselect.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/slim-select/1.23.0/slimselect.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="slim-multi-select" class="ms-class" multiple>
<option value="value 1">Some long value that doesn't fit here</option>
<option value="value 2">Value 2</option>
<option value="value 3">Another real long value that doesn't fit</option>
<option value="value 4">Value 4</option>
<option value="value 5">Value 5</option>
</select>

我不太擅长前端的东西,所以我希望得到一些帮助。

最佳答案

在 js 中进行以下更改将起作用:

$(".ss-content").on('mouseover', '.ss-option.scroll-text', function() {
$(this).removeClass("ellipsis");
var maxscroll = $(this)[0].scrollWidth;
var speed = maxscroll * 15;
$(this).animate({
scrollLeft: maxscroll
}, speed, "linear");
});
$(".ss-content").on('mouseout', '.ss-option.scroll-text', function() {
$(this).stop();
$(this).addClass("ellipsis");
$(this).animate({
scrollLeft: 0
}, 'slow');
});

代码不起作用的原因

您的代码只有在您单击任何项​​目后才有效。当您单击时,slimSlect删除所有 dom 并重新创建它,这也会删除附加到该 DOM 的事件。由于您的 DOM 和附加到它的事件被删除,mouseout/over事件没有成功。

我是如何解决这个问题的:

查看代码块,$(".ss-content").on('mouseout', '.ss-option.scroll-text' 。我已将事件附在此处.ss-content当你 mouserout/over它将搜索类 .ss-option.scroll-text这将永远存在。

如果您从父级进行选择,您的动态子 dom 将始终附加该事件。

@Nowshath 如何解决这个问题:他添加了beforeOpen and onChange调用方法 ScrollText 的钩子(Hook)每当您尝试打开或更改值时将事件注册到 dom


$('.ms-class > option').addClass('scroll-text ellipsis');

new SlimSelect({
select: '#slim-multi-select',
allowDeselectOption: true,
showContent: 'down',
})

$(".ss-content").on('mouseover', '.ss-option.scroll-text', function() {
$(this).removeClass("ellipsis");
var maxscroll = $(this)[0].scrollWidth;
var speed = maxscroll * 15;
$(this).animate({
scrollLeft: maxscroll
}, speed, "linear");
});
$(".ss-content").on('mouseout', '.ss-option.scroll-text', function() {
$(this).stop();
$(this).addClass("ellipsis");
$(this).animate({
scrollLeft: 0
}, 'slow');
});
.ms-class {
width: 200px !important;
}

.ms-class .ss-content {
z-index: 2;
}

.ms-class .ss-content .ss-list {
max-height: 100% !important;
}

.ms-class .ss-multi-selected .ss-values .ss-value {
font-size: 14px;
margin: 5px 5px 5px 0;
background-color: #884fa1;
}

.ms-class .ss-content .ss-list .ss-option,
.ms-class .ss-content .ss-list .ss-option-selected {
display: inline-block;
width: 33%;
}

.ms-class .ss-content .ss-list .ss-option.ss-hide,
.ms-class .ss-content .ss-list .ss-option.ss-hide {
display: none;
}

.ms-class .ss-content .ss-list .ss-option,
.ms-class .ss-content .ss-list .ss-option-selected {
display: inline-block;
width: 25%;
}

.ms-class .ss-content .ss-list .ss-option {
vertical-align: middle;
text-align: center;
height: 50px;
line-height: 0.9em;
white-space: nowrap;
overflow: hidden !important;
}

.ms-class .ss-content .ss-list .ss-option:after {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}

.ss-option:hover {
background-color: #884fa1 !important;
color: white;
}

.ms-class .ss-multi-selected .ss-values .ss-disabled,
.ms-class .ss-content .ss-search input {
font-family: 'Exo 2', 'Font Awesome 5 Free';
font-weight: 900 !important;
color: #444444 !important;
}

.ms-class .ss-multi-selected .ss-values .ss-disabled {
margin: 0 8px;
}

.ms-class .ss-content .ss-list .ss-option:only-child,
.ms-class .ss-content .ss-list .ss-option.ss-disabled:only-child {
width: 100% !important;
}

.ms-class .ss-content .ss-list .ss-option:first-child:nth-last-child(2),
.ms-class .ss-content .ss-list .ss-option:first-child:nth-last-child(2)~.ss-option {
width: 50% !important;
}

.ms-class .ss-content .ss-list .ss-option:first-child:nth-last-child(3),
.ms-class .ss-content .ss-list .ss-option:first-child:nth-last-child(3)~.ss-option {
width: 33% !important;
}

.ms-class .ss-content .ss-list .ss-option.ss-disabled:only-child {
width: 100% !important;
}

.ms-class .ss-multi-selected {
border-radius: 5px !important;
border: solid 1px #e8e8e8 !important;
min-height: 42px !important;
line-height: 100%;
-webkit-tap-highlight-color: transparent;
}

.ms-class .ss-multi-selected .ss-add {
display: flex;
flex: 0 1 3px;
margin: 14px 12px 0 5px;
}

.ms-class .ss-multi-selected .ss-add .ss-plus,
.ms-class .ss-multi-selected .ss-add .ss-plus:after {
background: #999;
}

.ms-class .ss-content .ss-list .ss-option:not(.ss-disabled).ss-option-selected {
background-color: rgba(136, 79, 161, 0.32);
}

.ms-class .ss-content .ss-list .ss-option:not(.ss-disabled).ss-option-selected:hover {
color: white !important;
}

.ellipsis {
text-overflow: ellipsis;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/slim-select/1.23.0/slimselect.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/slim-select/1.23.0/slimselect.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="slim-multi-select" class="ms-class" multiple>
<option value="value 1">Some long value that doesn't fit here</option>
<option value="value 2">Value 2</option>
<option value="value 3">Another real long value that doesn't fit</option>
<option value="value 4">Value 4</option>
<option value="value 5">Value 5</option>
</select>

关于javascript - JQuery 事件仅触发一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58157314/

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