最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to scroll diagonally with jQuery or Javascript - Stack Overflow

programmeradmin1浏览0评论

Are there projects or plugins that utilize javascript or jQuery to scroll diagonally?

e.g. when you scroll down your content, It would be pulled at the top-left corner of the browser; and when you scroll up your content would be pulled at the bottom-right of the corner.

I see some similar project/website that they animate their elements when scroll. Most of the site that use javascript has some lags with the effect though. Another i've seen is using html5 + parallax effect similar to "Nike a Better World" (/)

Can you point me where can be a good starting point? Basically I want to scroll the items diagonally left-or-right. If this can be done plainly in HTML5, I would highly consider that since I feel It would have less lag or less calculation being done.

Are there projects or plugins that utilize javascript or jQuery to scroll diagonally?

e.g. when you scroll down your content, It would be pulled at the top-left corner of the browser; and when you scroll up your content would be pulled at the bottom-right of the corner.

I see some similar project/website that they animate their elements when scroll. Most of the site that use javascript has some lags with the effect though. Another i've seen is using html5 + parallax effect similar to "Nike a Better World" (http://coding.smashingmagazine./2011/07/12/behind-the-scenes-of-nike-better-world/)

Can you point me where can be a good starting point? Basically I want to scroll the items diagonally left-or-right. If this can be done plainly in HTML5, I would highly consider that since I feel It would have less lag or less calculation being done.

Share Improve this question edited Mar 13, 2012 at 18:35 Pennf0lio asked Mar 13, 2012 at 18:18 Pennf0lioPennf0lio 3,8948 gold badges48 silver badges74 bronze badges 6
  • scroll right and down... – Ibu Commented Mar 13, 2012 at 18:39
  • 2 Something like this jsfiddle/j08691/K4YSq ? – j08691 Commented Mar 13, 2012 at 18:54
  • cool.. exactly like that :) appreciate the jsfiddle. I'll be borrwoing this snippet. – Pennf0lio Commented Mar 13, 2012 at 19:19
  • @j08691 though it breaks when I put some content on the div. – Pennf0lio Commented Mar 13, 2012 at 19:28
  • @j08691, please post your code in an answer below so it can be accepted after you fix it for the OP. – Sparky Commented Mar 13, 2012 at 23:33
 |  Show 1 more ment

2 Answers 2

Reset to default 6

I was able to create the effect that you wanted in a fiddle:

http://jsfiddle/t0nyh0/8QTYt/36/

Important Tidbits

  1. A "fixed" full-width and full-height wrapper that holds all your moving elements help you animate the div more consistently based on the scroll position (which is effectively the "keyframe" number).
  2. scroll_max, wrapper_width, and wrapper_height helps normalize the dimensions of wrapper. I.e. the very bottom of the scroll corresponds to the bottom/right of the wrapper, and the very top of the scroll corresponds with the top/left of the wrapper.
  3. Set your body's height to whatever number of "keyframes" that you want.
  4. To move from top left to bottom right on going down, adjust the top and left properties. For the reverse, adjust the bottom and right properties. Of course, you will need to formulate your own calculations for more plex animations, but know that doing $window.scrollTop() will give you the "keyframe" number.

HTML

<div id="wrapper">
    <div id="a">
        <h1>Meats</h1>
    </div>
    <div id="b">
        <h1>Veggies</h1>
        <hr/>
        <p>Veggies sunt bona vobis, proinde vos postulo esse magis daikon epazote peanut chickpea bamboo shoot rutabaga maize radish broccoli rabe lotus root kohlrabi napa cabbage courgette mustard squash mung bean.</p>
    </div>
</div>​

CSS

body
{
    height: 1000px; // 1000 keyframes
}

#wrapper
{
    width: 100%;
    height: 100%;
    position: fixed;
    border: 2px solid navy;
    overflow:hidden;
}

#a {
    position:absolute;
    background-color: #daf1d7;
    width: 300px;
    height: 300px;
}
#b
{
    position: absolute;
    background-color: #d2d0ee;
    width: 200px;
    height: 200px;
    bottom: 0px;
    right: 0px;
}

Javscript

var $window = $(window);
var $a = $('#a');
var $b = $('#b');

var scroll_max = document.documentElement.scrollHeight - document.documentElement.clientHeight;
var wrapper_height = $('#wrapper').height();
var wrapper_width = $('#wrapper').width();

$window.scroll(function() {
    console.log(scroll_max);
    $a.css({
        'top': ($window.scrollTop() / scroll_max) * wrapper_height,
        'left': ($window.scrollTop() / scroll_max) * wrapper_width
    });
    $b.css({
        'bottom': ($window.scrollTop() / scroll_max) * wrapper_height,
        'right': ($window.scrollTop() / scroll_max) * wrapper_width
    });
});​

Here's a potential solution for you (jsFiddle example):

jQuery:

$(window).scroll(function() {
    console.log($(this).scrollTop());
    $('#a').css({
        'width': $(this).scrollTop(),
        'height': $(this).scrollTop()
    });
    $('#b').css({
        'width': 300-$(this).scrollTop(),
        'height': 300-$(this).scrollTop()
    });
});

CSS:

#a,#b {
    position:fixed;
    background: orange;
}
#a{
    top:0;
    left:0;
}
#b {
    bottom:0;
    right:0;
    width:300px;
    height:300px;
}
body {
 height:2000px;   
}

HTML:

<div id="a"></div>
<div id="b"></div>
发布评论

评论列表(0)

  1. 暂无评论