jQuery循环 - 用鼠标滚轮控制?(jQuery cycle - control with mousewheel? (How to prevent mousewheel from continuously cycling?))
我正在使用jQuery循环与mousewheel插件结合使用。 我已经构建它,以便鼠标滚轮将根据轮子的移动方式在整个循环中移动。 但是,一旦周期发生变化,我想防止鼠标轮暂时在周期中移动一段时间,或者直到它在周期移动太快时向相反方向移动。
是否有任何方法可以阻止事件在每次触发后立即被触发?
我创建了一个JSBIN:
编辑:修复了鼠标滚轮的问题,现在它工作prev / next
-
编辑:在下面的r0m4n的帮助下,我设法找到了一个很好的解决方案。 下面的答案是好的,但在调用滚动之前会产生一些延迟,所以我决定创建一个可以绑定和反弹的函数。
function wheelMove(event,deltaY) { event.preventDefault(); $('#cycle').unbind('mousewheel', wheelMove); if (deltaY > 0) { $('#cycle').cycle('next'); } if (deltaY < 0) { $('#cycle').cycle('prev'); } } $(document).ready(function(){ $('#cycle').cycle({ fx: 'scrollVert', speed: 800, timeout: 0, after: function(){ interval = setTimeout(function(){ $('#cycle').bind('mousewheel', wheelMove); },1600); } }); $('#cycle').bind('mousewheel',wheelMove); });
I am using jQuery cycle in combination with a mousewheel plugin. I have built it so that the mousewheel will move through the cycle depending on how the wheel is moved. However, once the cycle changes, I would like to prevent the mousewheel from temporarily moving through the cycle for a moment or until it moves in the opposite direction as the cycle moves too quickly.
Is there any way to stop the event from being fired momentarily after it is fired each time?
I have created a JSBIN:
EDIT: Fixed an issue with the mousewheel, now it works prev/next
--
EDIT: With the help of r0m4n below, I managed to figure out a good solution. The answer below is good but creates a bit of delay before the scroll is called, so I decided to create a function that can be bound and rebound.
function wheelMove(event,deltaY) { event.preventDefault(); $('#cycle').unbind('mousewheel', wheelMove); if (deltaY > 0) { $('#cycle').cycle('next'); } if (deltaY < 0) { $('#cycle').cycle('prev'); } } $(document).ready(function(){ $('#cycle').cycle({ fx: 'scrollVert', speed: 800, timeout: 0, after: function(){ interval = setTimeout(function(){ $('#cycle').bind('mousewheel', wheelMove); },1600); } }); $('#cycle').bind('mousewheel',wheelMove); });
原文:https://stackoverflow.com/questions/9910816
最满意答案
我就是这样做的:将所有1×100向量垂直连接成一个矩阵,然后计算每列的平均值:
x = {[9 4 5 4] [5 3 6 7] [8 2 4 1]}; % example cell array of 3 vectors of size 1×4 result = mean(vertcat(x{:}), 1); % concatenate vertically. Mean of each column
在这个例子中,
result = 7.3333 3.0000 5.0000 4.0000
如果你真的想要避免使用矩阵,可以使用循环来累积值:
x = {[9 4 5 4] [5 3 6 7] [8 2 4 1]}; % example cell array of 3 vectors of size 1×4 result = zeros(size(x{1})); % initiallize for k = 1:numel(x) result = result + x{k}; % accumulate values end result = result / numel(x); % divide to obtain mean
This is how I would do it: concatenate all the 1×100 vectors vertically into a matrix, then compute the mean of each column:
x = {[9 4 5 4] [5 3 6 7] [8 2 4 1]}; % example cell array of 3 vectors of size 1×4 result = mean(vertcat(x{:}), 1); % concatenate vertically. Mean of each column
In this example,
result = 7.3333 3.0000 5.0000 4.0000
If you really want to avoid the matrix, you can use a loop to accumulate the values:
x = {[9 4 5 4] [5 3 6 7] [8 2 4 1]}; % example cell array of 3 vectors of size 1×4 result = zeros(size(x{1})); % initiallize for k = 1:numel(x) result = result + x{k}; % accumulate values end result = result / numel(x); % divide to obtain mean
相关问答
更多-
MATLAB计算矩阵中每列数对应的平均值?(MATLAB compute average value corresponding to each column number in matrix?)[2023-06-08]
unique可以生成第一列,而配备有mean函数的强大的accumarray将计算第二列(在这种情况下, nonzeros称为非存在的索引,如4 ): >> B = [unique(A(:,1)),nonzeros(accumarray(A(:,1),A(:,2),[],@mean))] B = 1.0000 5.0000 2.0000 6.5000 3.0000 5.0000 5.0000 7.0000 正如dan-man的评论中所指出的,只有当第 ... -
使用Matlab将4d矩阵重新整形为矢量单元阵列(using Matlab to reshape a 4d matrix into a cell array of vectors)[2022-11-25]
看看这是否是你想要的: x = randn(2,3,4,5); % example data x = reshape(x, [], size(x,4)); % collapse first three dimensions x = mat2cell(x, ones(1,size(x,1)), size(x,2)); % split first dimension into cells See if this is that you want: x = randn(2,3,4,5); % example d ... -
我就是这样做的:将所有1×100向量垂直连接成一个矩阵,然后计算每列的平均值: x = {[9 4 5 4] [5 3 6 7] [8 2 4 1]}; % example cell array of 3 vectors of size 1×4 result = mean(vertcat(x{:}), 1); % concatenate vertically. Mean of each column 在这个例子中, result = 7.3333 3.0000 5.0000 4. ...
-
编辑: 对于像您的情况一样的行向量单元格,这将使用零填充向量以形成矩阵 out=cell2mat(cellfun(@(x)cat(2,x,zeros(1,maxLength-length(x))),C,'UniformOutput',false)) out = 1 2 3 0 0 1 2 3 4 5 1 2 3 4 0 今天早些时候也提出了一个类似的问题 ,虽然问题的措辞略有不 ...
-
您可以将数据插入另一个单元格,而不是将数据插入到普通矩阵中 Ecell=num2cell(E); Ecell(:,3)=uID; Instead of inserting the data into a normal matrix, you can insert it into another cell Ecell=num2cell(E); Ecell(:,3)=uID;
-
cell2mat应该工作。 根据您的需要,您可能需要转置 : >> m1 = [1 2; 3 4]; >> m2 = [11 12; 13 14]; >> m3 = [21 12; 23 14]; >> m4 = [31 32; 33 34]; >> myCell = {m1, m2; m3 m4}; >> cell2mat(myCell) ans = 1 2 11 12 3 4 13 14 21 12 31 32 ...
-
您可以在每个单元阵列上调用num2cell ,然后使用cat将它们连接起来。 %// Generate some test data a = arrayfun(@(x)rand(14, x), [16 17 27 62 16] , 'uni', 0); [14x16 double] [14x17 double] [14x27 double] [14x62 double] [14x16 double] %// Now merge them into a cell array of vect ...
-
从单元阵列中的序列生成倒数矩阵(Matlab)(Generate reciprocal matrices from sequences in the cell array ( Matlab ))[2023-09-16]
此任务有2个部分(对于单元格数组的每个元素):1)从字符串中获取数字。 例如,ACB序列转换为[3,1,2],因为第一列的强度为3,第二列为1,第三列为2。2)从该数组创建矩阵。 1)检查哪个字母是第一个,并将3放在正确的位置。 然后检查哪个是第二个,放2.第三个,放1.如果没有数字,则输入nan(= nan初始化数组)。 如果你有任意多个字母的东西是一样的,那么首先要弄清楚你需要多大的数组。 但是你的第3个案例将停止工作,因为它假设你只有2个元素,并将制作2x2矩阵。 第二部分是这样的: seq=[3,1 ... -
Matlab:在矩阵中存储数组?(Matlab: store array in matrix?)[2022-05-01]
如果矢量长度不同,英雄的建议就是你想要的。 但假设元素的数量是常数(它们似乎是),您也可以使用三维数组,其中每个(i,j)元素在第三维中包含一个向量,如下所示: rows = 5; cols = 5; n = 10; %// Dimensions matrix = zeros(rows, cols, n); %// Initialize matrix vector = 1:n; %// Just an example for ii = 1:rows ... -
您可以使用squeeze删除冗余单例维度。 b = squeeze(mat2cell(a, [192],[88],[1 1 1])); You can use squeeze to remove redundant singleton dimensions. b = squeeze(mat2cell(a, [192],[88],[1 1 1]));