gpt4 book ai didi

javascript - 强制向左滚动时如何防止闪烁?

转载 作者:太空狗 更新时间:2023-10-29 13:29:11 25 4
gpt4 key购买 nike

我上次问this kind of question ,但我没有从这些答案中找到更好的解决方案。

我试过了。当滚动位置到达 te 点 500 到 600 时,以前的 div 标签从我的容器中分离出来,新的 div 标签附加在右侧。

但是在 block 删除、附加和左位置更改之后,我对容器的 scrollLeft 有一个问题。在我的脚本中,在出现闪烁的 scrolleft:100 点强制滚动到左侧。强制滚动到左侧时如何停止闪烁。? jsfiddle

最佳答案

很难从你的问题中准确判断出你在寻找什么。我假设你想要一个带有延迟加载图像的无限水平滚动。

我认为没有理由删除图像,除非您不想滚动回它们。如果您不知道初始图像宽度,您可以在滚动时动态更改滚动条的宽度,但您还需要调整滚动位置。

这是一个代码片段来说明:

  $(function() {

var scrollContainer = $("#container");
var scrollContents = $("#scroll-contents");
var colors = ['rgba(143, 146, 199, 0.49)', 'rgba(199, 143, 186, 0.49)', 'rgba(149, 199, 143, 0.49)', 'rgba(229, 86, 61, 0.49)',
'rgba(212, 229, 61, 0.49)', 'rgba(206, 61, 229, 0.49)', 'rgba(229, 157, 61, 0.49)', 'rgba(61, 165, 229, 0.49)', 'rgba(61, 229, 133, 0.49)',
'rgba(229, 61, 61, 0.49)', 'rgba(116, 61, 229, 0.49', 'rgba(218, 229, 61, 0.49)', 'rgba(21, 43, 157, 0.49)', 'rgba(153, 157, 21, 0.49)', 'rgba(199, 143, 186, 0.49)',
'rgba(149, 199, 143, 0.49)', 'rgba(229, 86, 61, 0.49)', 'rgba(212, 229, 61, 0.49)', 'rgba(206, 61, 229, 0.49)', 'rgba(229, 157, 61, 0.49)', 'rgba(61, 165, 229, 0.49)',
'rgba(61, 229, 133, 0.49)', 'rgba(229, 61, 61, 0.49)', 'rgba(116, 61, 229, 0.49', 'rgba(218, 229, 61, 0.49)', 'rgba(21, 43, 157, 0.49)', 'rgba(153, 157, 21, 0.49)',
'rgba(199, 143, 186, 0.49)', 'rgba(149, 199, 143, 0.49)', 'rgba(143, 146, 199, 0.49)', 'rgba(199, 143, 186, 0.49)', 'rgba(149, 199, 143, 0.49)', 'rgba(229, 86, 61, 0.49)',
'rgba(212, 229, 61, 0.49)', 'rgba(206, 61, 229, 0.49)', 'rgba(229, 157, 61, 0.49)', 'rgba(61, 165, 229, 0.49)', 'rgba(61, 229, 133, 0.49)', 'rgba(229, 61, 61, 0.49)',
'rgba(116, 61, 229, 0.49', 'rgba(218, 229, 61, 0.49)', 'rgba(21, 43, 157, 0.49)', 'rgba(153, 157, 21, 0.49)', 'rgba(199, 143, 186, 0.49)', 'rgba(149, 199, 143, 0.49)',
'rgba(229, 86, 61, 0.49)', 'rgba(212, 229, 61, 0.49)', 'rgba(206, 61, 229, 0.49)', 'rgba(229, 157, 61, 0.49)', 'rgba(61, 165, 229, 0.49)', 'rgba(61, 229, 133, 0.49)',
'rgba(229, 61, 61, 0.49)', 'rgba(116, 61, 229, 0.49', 'rgba(218, 229, 61, 0.49)', 'rgba(21, 43, 157, 0.49)', 'rgba(153, 157, 21, 0.49)', 'rgba(199, 143, 186, 0.49)',
'rgba(149, 199, 143, 0.49)'
];

function addBlock(blockNum) {
scrollContents.append('<div class="block block' + blockNum + '" style="background:' + colors[blockNum] + '"><h4>' + (blockNum + 1) + '</h4></div>');
document.getElementById('cont').innerHTML = blockNum + 1;
}

var totalBlocks = colors.length;
var numBlocks = 6;

scrollContents.width(totalBlocks * 400);

for (var iBlock = 0; iBlock < numBlocks; iBlock++) {
addBlock(iBlock);
}

scrollContainer.scroll(function() {
if (numBlocks != totalBlocks && (scrollContainer[0].scrollLeft > (numBlocks - 5) * 400)) {
addBlock(++numBlocks);
}
});

});
#container {
width: 100%;
height: 350px;
position: relative;
overflow: scroll;
background: white;
top: 50px;
overflow: hidden;
}
.block {
display: inline-block;
width: 400px;
height: 350px;
}
#scroll-contents {
height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/asvd/dragscroll/master/dragscroll.js"></script>

<body>
<div id="container" class="dragscroll">
<div id="scroll-contents">
</div>
</div>
<div id="cont" style="margin-top:150px; background:blue; height:20px; width:50px; color:white;">
</div>
</body>

关于javascript - 强制向左滚动时如何防止闪烁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30502245/

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