- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过调用函数将溢出 flexbox 容器的内容从一个 div 水平滚动到另一个 div。
HTML #href 和 jQuery window.location 实现了我的目的,但它们还将容器滚动到窗口的顶部,而不是只滚动容器的内容而不改变窗口位置。
我认为解决方案是:
var currentSlideFocus = $("#select_slide_" + currentSlide).position().left;
$("#slideWindow").scrollLeft(currentSlideFocus);
然而,currentSlideFocus 的值只有我预期的 10% 左右,scrollLeft 的滚动行为量似乎只有 currentSlideFocus 的 10% 左右。给了什么?
position().left 测量的是元素左侧与其容器左侧之间的距离,还是我对它的解释不正确?
这是下面骨架化元素的 JSfiddle。谢谢。 https://jsfiddle.net/goneZoe/sf46ea03/
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var currentSlide = 1;
var slideLink = 1;
var totalSlides;
var currentSlideFocus;
var slideIntvl;
var slideVW;
jQuery(function() {
totalSlides = $('.slide_frame').length;
$(".current_slide").html(currentSlide);
$(".total_slides").html(totalSlides);
$(".position_left").html(currentSlideFocus);
$(makeControlLinks);
$("#select_slide_" + currentSlide).addClass('active_slide_control');
$(slideSlider);
})();
/* slide every 5 seconds, pause on mouse hover, resume after mouseout */
function slideSlider(){
slideIntvl = setInterval(slideNext, 5000);
$(".slider_wrapper").hover(function() {
clearInterval(slideIntvl);
}, function() {
slideIntvl = setInterval(slideNext, 5000);
});
}
/* previous and next controls */
function slideNext(){
if (currentSlide < totalSlides) {
nextSlide = currentSlide + 1;
} else {
nextSlide = 1;
}
currentSlide = nextSlide;
$(changeSlide);
}
function slidePrev(){
if (currentSlide == 1) {
prevSlide = totalSlides
} else {
prevSlide = currentSlide - 1;
}
currentSlide = prevSlide;
$(changeSlide);
}
/* formula to change that slide when the time comes */
function changeSlide(){
var currentSlideFocus = $("#select_slide_" + currentSlide).position().left;
$("#slideWindow").scrollLeft(currentSlideFocus);
$(".active_slide_control").removeClass('active_slide_control');
$("#select_slide_" + currentSlide).addClass('active_slide_control');
$(".current_slide").html(currentSlide);
$(".position_left").html(currentSlideFocus);
}
/* --------- generate skip-to links ---------- */
function makeControlLinks(){
if (slideLink <= totalSlides){
$("#frame_select").append('<a id="select_slide_'+slideLink+'" class="selector" onclick="selectSlide'+slideLink+'()">'+slideLink+'</a>');
slideLink = slideLink + 1;
$(makeControlLinks);
} else {
return false;
}
}
/* individual jump-to specific controls */
function selectSlide1(){
currentSlide = 1;
$(changeSlide);
}
function selectSlide2(){
currentSlide = 2;
$(changeSlide);
}
function selectSlide3(){
currentSlide = 3;
$(changeSlide);
}
function selectSlide4(){
currentSlide = 4;
$(changeSlide);
}
function selectSlide5(){
currentSlide = 5;
$(changeSlide);
}
function selectSlide6(){
currentSlide = 6;
$(changeSlide);
}
function selectSlide7(){
currentSlide = 7;
$(changeSlide);
}
function selectSlide8(){
currentSlide = 8;
$(changeSlide);
}
function selectSlide9(){
currentSlide = 9;
$(changeSlide);
}
</script>
<style type ="text/css">
#slider_wrapper_container {
position: relative;
display: block;
width: 100vw;
left: -24.15vw;
height: auto;
}
.slider_wrapper {
position: relative;
display: block;
margin: auto;
margin-top: 50px;
margin-bottom: 50px;
width: 65vw;
height: calc(36.562vw + 66px);
border: 1px solid rgb(51, 51, 51);
overflow: hidden;
}
.slide_window {
display: flex;
flex-direction: row;
scroll-behavior: smooth;
overflow-x: hidden;
}
.slide_frame {
position: relative;
display: block;
width: 100%;
height: 100px;
flex-shrink: 0;
border: 2px solid blue;
}
.slide_frame p{
position: relative;
display: block;
width: 1em;
margin: auto;
font-size: 2em;
}
#frame_select {
position: relative;
display: block;
margin: auto;
padding-top: 20px;
text-align: center;
width: 85%;
}
.slider_controls {
position: absolute;
display: block;
height: 66px;
width: 100%;
bottom: 0px;
}
.selector {
padding: 5px;
text-align: center;
text-decoration: none;
margin: 5px;
border: 1px solid rgb(91, 91, 91);
cursor: pointer;
}
.active_slide_control {
background-color: black;
color: white;
cursor: default;
}
#left_arrow, #right_arrow {
position: absolute;
display: inline;
height: 100%;
padding: 10px;
padding-top: 20px;
cursor: pointer;
}
#left_arrow {
float: left;
}
#right_arrow {
float: right;
right: 0px;
top: 0px;
}
#right_arrow {
float: right;
border-bottom-right-radius: 40px 20px;
}
#left_control, #right_control {
height: 50%;
}
</style>
</head>
<body>
<div class="slider_wrapper">
<div id="slideWindow" class="slide_window">
<div id="slide_1" class="slide_frame">
<p>1</p>
</div>
<div id="slide_2" class="slide_frame">
<p>2</p>
</div>
<div id="slide_3" class="slide_frame">
<p>3</p>
</div>
<div id="slide_4" class="slide_frame">
<p>4</p>
</div>
<div id="slide_5" class="slide_frame">
<p>5</p>
</div>
</div>
<div class="slider_controls">
<div id="left_arrow" onclick="slidePrev()">
<a><--</a>
</div>
<div id="frame_select">
</div>
<div id="right_arrow" onclick="slideNext()">
<a>--></a>
</div>
</div>
</div>
<a id="readout">Current slide is #<span class="current_slide">?</span> out of <span class="total_slides">?</span> slides. The position.left value of currentSlide is <span class="position_left">?</span></a>
</body>
</html>
最佳答案
我有点弄清楚了为什么我从 position().left 得到结果,而不是仔细研究 CSS,我重新考虑了我使用变量的方式并去了这条路线,而不是。就像一个魅力。
singleSlideWidth = $(".slide_frame").width();
slideMeasure = currentSlide - 1;
currentSlideDistance = singleSlideWidth * slideMeasure;
$("#slideWindow").scrollLeft(currentSlideDistance);
关于jquery - 如何将 ".position().left"的值和 "scrollLeft"的行为应用于溢出容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54680683/
在带有 jQuery 的 CoffeeScript 中,以下语句有什么区别吗? jQuery ($) -> jQuery -> $ - > 最佳答案 第一个与其他两个不同,就像在纯 JavaScr
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭13 年前。 Improve th
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
这个问题可能听起来很愚蠢,但请耐心等待,因为我完全是初学者。我下载了两个 jQuery 版本,开发版本和生产版本。我想知道作为学习 jQuery 的初学者,什么更适合我。 最佳答案 如果您对 jQue
The documentation说要使用 1.6.4,但我们现在已经升级到 1.7.2。 我可以在 jQuery Mobile 中使用最新版本的 jQuery 吗? 最佳答案 您当然可以,但如果您想
我在这里看到这个不错的 jquery 插件:prettyphoto jquery lightbox有没有办法只用一个简单的jquery来实现这样的效果。 我只需要弹出和内联内容。你的回复有很大帮助。
很明显我正在尝试做一些 jQuery 不喜欢的事情。 我正在使用 javascript 上传图片。每次上传图片时,我都希望它可见,并附加一个有效的删除脚本。显示工作正常,删除则不然,因为当我用 fir
这两个哪个是正确的? jQuery('someclass').click(function() { alert("I've been clicked!"); }); 或 jQuery('somec
我正在寻找一个具有以下格式的插件 if (jQuery)(function ($) { -- plugin code -- })(jQuery); 我明白 (function ($)
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭10 年前。 Improv
我知道这个问题已经被问过几次了,但想知道您是否可以帮助我解决这个问题。 背景:我尝试创建一个使用 Ajax 提交的表单(jQuery 表单提交)。我已经工作得很好,然后我想在表单上得到验证。我可以使用
我正在使用无处不在的jquery validate plugin用于表单验证。它支持使用metadata plugin用于向表单元素添加验证规则。 我正在使用此功能。当验证查找这些规则时,它会对元素进
我更喜欢为我一直在开发的网络社区添加实用的视觉效果,但随着事情开始堆积,我担心加载时间。 拥有用户真的更快吗加载(希望是缓存的)副本来自 Google 存储库的 jquery? 是否使用 jQuery
这个问题已经有答案了: Slide right to left? (17 个回答) 已关闭 9 年前。 你能告诉我有没有办法在 jQuery 中左右滑动而不使用 jQuery UI 和 jQuery
我如何找出最适合某种情况的方法?任何人都可以提供一些示例来了解功能和性能方面的差异吗? 最佳答案 XMLHttpRequest 是原始浏览器对象,jQuery 将其包装成一种更有用和简化的形式以及跨浏
运行时 php bin/console oro:assets:build ,我有 11 个这样的错误: ERROR in ../node_modules/jquery-form/src/jquery.
我试图找到 jQuery.ajax() 在源代码中的定义位置。但是,使用 grep 似乎不起作用。 在哪里? 谢谢。 > grep jQuery.ajax src/* src/ajax.js:// B
$.fn.sortByDepth = function() { var ar = []; var result = $([]); $(this).each(function()
我的页面上有多个图像。为了检测损坏的图像,我使用了在 SO 上找到的这个。 $('.imgRot').one('error',function(){ $(this).attr('src','b
我在理解 $ 符号作为 jQuery 函数的别名时遇到了一些麻烦,尤其是在插件中。你能解释一下 jQuery 如何实现这种别名:它如何定义 '$' 作为 jQuery 函数的别名?这是第一个问题。 其
我是一名优秀的程序员,十分优秀!