- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想在我的 jQuery 脚本中创建一个非常简单的函数。当手指/光标触摸/点击屏幕时,我希望页面跟随手指/光标的移动水平滑动。我知道有很多人创建了很多插件,但我真的不需要其他人的解决方案。该图像是我的 HTML 外观的直观 View 。这真的很简单。
jQuery sciprt 显然不正确,但我希望它能让您了解我需要的简单函数。我没有额外的类(class)或淡入淡出功能或任何东西。
$(document).live('touchmove' or 'mousemove', function() {
$('div[class=page_*], div[class=page_^]').[follow movements horizontally, and auto align to nearest edge when let go.];
});
此外,我希望能够对一个大的 div 执行相同的操作,因此元素移动的宽度变量可能应该等于 $(window).width();
。事实上,我认为那将是最好的主意。我总是可以在大 div 中放入更多内容并使其变大,所以请保留它。它应该更简单,只关注一个元素。
最佳答案
所以,这是我的解决方案。我做了一些更改,现在您可以拥有 3 个以上的页面。此外,我定义了一个名为 threshold 的变量,设置为页面的一半。如果您希望阈值大于或小于页面的 hakf,则必须进行更多更改。
HTML 代码:
<div class="container">
<div class="wrap">
<div class="page page1"></div>
<div class="page page2"></div>
<div class="page page3"></div>
<div class="page page4"></div>
</div>
</div>
CSS 代码:
.container, .page, .wrap {
width: 300px;
height: 400px;
}
.container {
background: #efefef;
box-shadow: 0px 0px 10px black;
overflow: hidden;
position: relative;
margin: 5px auto;
}
.wrap {
width: 1200px;
position: absolute;
top: 0;
left: 0;
}
.page {
float: left;
display: block;
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
-o-user-select: none;
}
.page1 {
background: yellow;
}
.page2 {
background: green;
}
.page3 {
background: blue;
}
.page4 {
background: red;
}
关于 CSS 代码,请记住,如果您想更改页面 大小,您还必须更改container 和wrap 尺寸。
JS 代码:
var mouseDown = false, right;
var xi, xf, leftX = 0;
var nPages = $(".page").size();
var pageSize = $(".page").width();
var threshold = pageSize/2;
var currentPage = 0;
$(".container").on("mousedown", function (e) {
mouseDown = true;
xi = e.pageX;
});
$(".container").on("mouseup", function (e) {
if (mouseDown) {
mouseDown = false;
xf = e.pageX;
leftX = parseInt($(".wrap").css("left").split("px")[0]);
if ((e.pageX - xi) < -threshold || (e.pageX - xi) > threshold) {
setFocusedPage();
} else {
restore();
}
}
});
$(".container").on("mouseleave", function (e) {
if (mouseDown) {
mouseDown = false;
xf = e.pageX;
leftX = parseInt($(".wrap").css("left").split("px")[0]);
if ((e.pageX - xi) < -threshold || (e.pageX - xi) > threshold) {
setFocusedPage();
} else {
restore();
}
}
});
$(".container").on("mousemove", function (e) {
if (mouseDown) {
$(".wrap").css({
"left": (leftX + (e.pageX - xi))
});
right = ((e.pageX - xi) < 0) ? true : false;
}
});
function restore() {
$(".wrap").stop().animate({
"left": -(currentPage * pageSize)
}, 200, function () {
leftX = parseInt($(".wrap").css("left").split("px")[0]);
});
}
function setFocusedPage() {
if (leftX >= (-threshold)) { // First Page
currentPage = 0;
} else if (leftX < (-threshold) && leftX >= (-(nPages + 1) * threshold)) { // Second to N-1 Page
(right) ? currentPage++ : currentPage--;
} else if (leftX < -((nPages + 1) * threshold)) { // Third Page
currentPage = nPages - 1;
}
$(".wrap").stop().animate({
"left": -(currentPage * pageSize)
}, 200, function () {
leftX = parseInt($(".wrap").css("left").split("px")[0]);
});
}
请记住,如果您想要不同的阈值,您将必须进行一些更改,尤其是在 setFocusedPage()
函数中。
这是我最后一个DEMO
关于javascript - jQuery - Div 跟随 touchmove/mousemove + autoalign,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14714615/
我试图在我的数据库中找到所有关注该用户的用户。 followers 集合有 3 个字段:_id、_t、_f。 当一个用户关注另一个用户时,它会将他们的用户 ID 添加到 _f 并将目标用户的 ID 添
我有一个 UICollectionView,它由单元格中的 UIImagePickerControl 按钮填充,我希望跟随 Collection View 末尾的单元格通过屏幕,但仍允许用户滚动 -
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 个月前。 Improv
好的,所以我知道如何将文本剪裁成特定的几何形状,但是文本不会根据剪裁自动换行,那么如果您有“勾选”作为几何/路径? 是否需要手动添加适合每一行的文本框,然后根据适合/不适合的内容拆分文本? 最佳答案
我里面有链接 s,但我在所有 上也有一个点击事件s。代码如下所示: $( document ).ready( function() { $( 'td.event' ).click( func
短版: 是)我有的: 2 元组列表,例如 [("a", "b"), ("b", "c"), ("d", "e"), ("c", "d"), ("f", "g")]不一定按字母顺序排列 我想要的是: 给
我正在尝试使用 xpath 来获取表的内容。 表格看起来像这样 Stuff Contents Contents Stuff
我有几个像这样的div: My Link 问题是,如果用户单击 div 而不是文本,则链接不会触发。这是我到目前为止所拥有的: $('#TopMenuBar .MenuList').click
我想要做一个链接到这个 .after() 的 .fadeIn() 动画,但这似乎不是这样做的方法。有什么建议吗? $(clicked_item).parent().parent().parent().
int getIdForSong(Song song){ String selectQuery = "SELECT id FROM " + TABLE_SONG + " WHERE " + S
现在右侧的 div 一直跟随滚动。如果我想让它在页面滚动到div的顶部时开始跟随滚动,并在向上滚动时让它保持在那里,我还需要做什么? jsfiddle $(window).scroll(functio
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and t
我正在为我的客户设计一件 T 恤,我使用 html5 Canvas 制作了它。衬衫设计师现在已经完成,但他要求我添加一个放大镜(类似这样的东西:http://mlens.musings.it/)。我在
我正在尝试调试远程主机上的 fork 进程,但每次都让 gdbserver 进程在子退出时结束。 尝试在 .gdbinit 中设置“set follow-fork-mode child”,没有帮助。
我正在制作一个带有滚动控件的响应式菜单。我有一个小问题,我的渐变和控件跟随,滚动时,你可以在我的 JSFiddle 中看到它. 我的菜单控件有我的 CSS #page .page-nav .contr
我将我的页面分为左右两部分。我用 div 来制作左导航和右导航。在右侧导航中,我也有多个 div。现在,当我向下滚动页面时,只有页面的右侧部分正在滚动,而左侧导航则停留在那里。 所以当我向下滚动页面时
我刚刚开始使用 svg 和 anime.js。我正在尝试重新创建 svg motion path在文档中找到我自己的 Assets 。不过,我没有使用 div 来跟随路径,而是使用了另一条路径。 我有
我将展示整个代码,但请注意指针上方的引号,因为我将在那里讨论我的问题。我会在引用评论中告诉你我认为那里发生了什么。代码编译并运行,我只需要帮助理解部分代码。 #include #include i
我在左侧有一个 div,其中包括营业时间和天气。我希望该 div 根据用户的滚动方式向下和向上滚动。所以它会跟随页面上下移动。我将如何尝试呢?这是我的网站judystropicalgarden.com
所以我遇到了这个小问题,我的相机错误地固定在播放器上。 左上角的蓝色 Sprite 是玩家,但它应该位于屏幕中央。关于这个问题的所有其他线程都使用固定渲染管道,而我使用基于 VBO 的线程。 我的矩阵
我是一名优秀的程序员,十分优秀!