更改scrollTo功能以使下一个/上一个图像位于中心而不是左侧
Change scrollTo function to make next/prev image come to center instead of the left
我有一个水平滚动的div,其中包含图像。
使用一些jQuery,我拥有它,因此单击div的右半部分滚动到下一个图像,左半部分滚动到上一个图像。目前,在scrollTo功能之后,下一个/上一个图像位于容器的左侧。
如何更改它以使其位于容器的中心,并更改 currentElement 函数以检查图像何时居中。
$(document).ready(function(){
var container = $("#sg-scroll"),
child = $(".sg-pic_wrap"),
current;
// CURRENT ELEMENT CHECK
function currentElem () {
child.each(function(){
var x = $(this).offset().left,
w = $(this).width();
if (x <= 0 && x < w ) {
current = $(this);
}
})
}
// MOVE TO ELEM
function scrollTo (elem) {
container.scrollLeft(elem);
}
// CLICK
container.on("click", function(e){
var _w = $(window).width();
if ((e.pageX) <= (_w/2)) { // LEFT
currentElem();
if (current.prev().length > 0) { // otherwise we get undefined error
scrollTo( parseInt((container.scrollLeft()) + (current.prev().offset().left) + 5 ) );
}
}
else { // RIGHT
currentElem();
scrollTo( parseInt((container.scrollLeft()) + (current.next().offset().left) + 5 ) );
}
});
// CURSOR ...
container.on("mousemove", function(e){
var _w = $(window).width();
if ((e.pageX) <= (_w/2)) { // LEFT
}
else { // RIGHT
}
});
});//document.ready
这是在画廊中使用的小提琴。
在 JSFiddle 中查看我的// CHANGED
注释。
$(document).ready(function() {
// cache here reusable elements
var container = $("#sg-scroll"),
child = $(".sg-pic_wrap"),
current;
// CURRENT ELEMENT CHECK
function currentElem() {
// .each() instead of for(i = 0; i < var;i++)
child.each(function() {
// these variables must be updated each time
// we launch the function, so we cache them inside
var x = $(this).offset().left,
w = $(this).width(),
cW = $('#sg-container').width() / 2; // CHANGED
if (x < cW && x + w > cW) { // CHANGED
current = $(this);
}
})
}
// MOVE TO ELEM
function scrollTo(elem, width) {
elem = elem + (width/2) - ($('#sg-container').width()/2); // CHANGED
container.scrollLeft(elem);
}
// CLICK
container.on("click", function(e) {
var _w = $(window).width();
if ((e.pageX) <= (_w / 2)) { // LEFT
// we only need to check the current element each time we click, not on scroll
// doing this will result in faster js
currentElem();
if (current.prev().length > 0) { // otherwise we get undefined error
//scrollTo( parseInt((container.scrollLeft()) + (current.prev().offset().left) + 5) );
scrollTo(container.scrollLeft() + current.prev().position().left, current.prev().width()); // CHANGED
}
} else { // RIGHT
currentElem();
//scrollTo( parseInt((container.scrollLeft()) + (current.next().offset().left) + 5 ) );
scrollTo(container.scrollLeft() + current.next().position().left, current.next().width()); // CHANGED
}
});
// CURSOR ...
container.on("mousemove", function(e) {
var _w = $(window).width();
if ((e.pageX) <= (_w / 2)) { // LEFT
//change cursor here
} else { // RIGHT
// or here
}
});
}); //document.ready
相关文章:
- 通过传递图像的URL通过javascript读取/处理图像-类似于PHP中的file_get_contents
- 如何在RoyalSlider中设置中心图像宽度
- 查找仅适用于原始图像的图像放大算法的名称
- 我如何完善这个Javascript代码,使其只适用于来自图像的链接(而不是来自文本的链接)
- 简单jquery缩放插件中的中心图像
- Chrome壁纸API适用于某些图像,不适用于其他图像,没有明显的原因
- 如何将拖动应用于矩形/图像
- 在鼠标悬停时将中心图像更改为较小的图像
- 图像名称返回为未定义,但仅适用于某些图像
- 将鼠标悬停在图像上以显示文本框,使用 JavaScript 仅适用于一个图像,因为我使用的是 getElementByI
- 满足于获取图像
- 使javascript通用,以便它可以应用于所有图像
- 如何在html5画布上移动相对于背景图像的反向缩放组
- 为什么我的jQuery悬停函数只适用于一个图像
- 在褪色的背景中心图像
- 是否有类似于Facebook's图像裁剪的jQuery图像裁剪插件?
- CSS3的过渡只适用于背景图像
- 放大后的Jquery中心图像溢出:自动容器
- 根据浏览器大小改变大小的中心图像
- Raphael.js如何获得纸张的中心点位置和相对于中心的比例