在溢出:无元素中自动滚动内容(jQuery) [英] Scroll content automatically in an overflow:none element (jQuery)

查看:92
本文介绍了在溢出:无元素中自动滚动内容(jQuery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在overflow:none元素中混合了内容(图像,文本).现在,我想根据鼠标指针的位置在x/y轴上自动滚动该内容.溢出:自动不会是一种选择,因为我不想显示/使用该元素中的滚动条.

I have mixed content (images, text) in an overflow:none element. Now, I'd like to automatically scroll that content in x/y axis based on the location of the mouse pointer. Overflow:auto wouldn't be an option, as I wouldn't like to show/use the scrollbars in that element.

我发现一个脚本可以执行类似的操作,但是只对背景图像起作用.除了移动div的全部内容,是否有办法产生类似的效果?谢谢您的提前答复!

I've found a script which does something similar, but only with the background image. Is there a way to have a similar effect but with moving the whole content of the div? Thank you for your answers in advance!

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Test jQuery Move Background with Mouse Move</title>
  <link rev="made" href="mailto:covertlinks [ at ] gmail [ dot ] com" />
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  <meta name="generator" content="NoteTab Pro 5.5" />
  <meta name="author" content="Perry Wolf" />
  <meta name="description" content="" />
  <meta name="keywords" content="" />
<script type="text/javascript" src="jquery.1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    var vH=$('#viewer').height();
    var vW=$('#viewer').width();
    var vT=$('#viewer').offset().top;
    var vL=$('#viewer').offset().left;
    $('#viewer').mousemove(function(e){
        var ypos=e.pageY-vT;
        var xpos=e.pageX-vL;
        var y=Math.round(ypos/vW*100);
        var x=Math.round(xpos/vH*100);
        $('#test').val(x+' , '+y);
        $('#viewer').css({backgroundPosition: x+'% '+y+'%'});
    });
});
</script>
</head>
<body style="color:#FFFFFF;background:#102030;text-align:center;">
<h1 style="text-align:center;">Test Move Background on Mousemove:</h1>
<div id="viewer" style="border:solid 1px #FFFFFF;margin:50px auto 0px auto;width:400px;height:400px;background:url(ironhide1024x768.jpg) 0% 0% no-repeat;cursor:url(target_cursor.gif), crosshair;text-align:center;line-height:300px;">
</div>
<input type="text" id="test" size="30" style="display:block;margin:10px auto;width:150px;" />
</body>
</html>

推荐答案

这很有趣!更改它,以便移动scrollTopscrollLeft而不是背景位置.由于您有百分比,因此您可以像这样计算scrollTopscrollLeft的. 查看小提琴.

This was a fun one! Change it so you move the scrollTop and scrollLeft instead of background position. Since you have a percentage you could calculate the of scrollTop and scrollLeft like so. Check out the fiddle.

$(document).ready(function(){
    var viewer = $('#viewer'),
        vH = viewer.height(),
        vW = viewer.width(),
        vT = viewer.offset().top,
        vL = viewer.offset().left,
        sTop = viewer.find(':first').height() + 18 - vH,
        sLeft = viewer.find(':first').width() + 18 - vW;
    // the sTop and sLeft could be calculated differently. In this case 
    // I am assuming that the viewer has a single child that is larger than itself.
    // realistically this should check total possible scrollTop and scrollLeft

    $('#viewer').mousemove(function(e){
        var $this = $(this),
            y = (e.pageY-vT)/vH,
            x = (e.pageX-vL)/vW;
        $this.scrollTop(Math.round(sTop * y))
            .scrollLeft(Math.round(sLeft * x));
    });
});

这篇关于在溢出:无元素中自动滚动内容(jQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆