2016-04-03 17 views
0

イメージが上にあるウェブページを作成しようとしています。ユーザーがスクロールアップすると、画像は少し上に上がって、他の要素はブロックされます。その画像が所望の位置に達するとすぐに、残りの要素も同様にスクロールアップまたはスクロールすることができる。これが主なアイデアです:ユーザーはスクロールし、イメージを動かします。ユーザーはスクロールを保持し、要素も移動します。 これは私がこれまで持っているものです。1つの要素でスクロール作業を行う方法は?

var lastScrollTop = 0; 

$(window).scroll(function(event){ 
    var st = $(this).scrollTop(); 

    if (st > lastScrollTop){ 
    $("#ballonImg").css("top", "-=5px"); 
    } else { 
     $("#ballonImg").css("top", "+=5px"); 
    } 
    lastScrollTop = st; 
}); 

とHTML:

<div id="top"> 
    <div id="images"> 
    <img id="ballonImg"src="http://i.imgur.com/rMm5gCy.jpg"/> 
    <img src="http://i.imgur.com/l3GI35D.png"/> 
    </div> 
    </div> 

    <div id="cenas"></div> 
+0

jsfiddleは、これは私のcodepenある –

+0

素晴らしいことだ:http://codepen.io/Nicki_Reds/pen/jqGeEm –

答えて

0

あなたはCSSのポジショニングを使用する必要があります。位置固定、絶対、相対、および静的は、すべてこの目的に役立ちます。動きのない画像をdivの背景として設定し、その内部にもう1つの画像を置いて、Z-インデックスを2(または1以上のもの)に設定することも考慮する必要があります。

0

私はこれを作成しました。あなたと同じように動作します。

mousewheelイベントを取得するのにjquery-mousewheelを使用していて、deltaYを使用して、ユーザーがスクロールしているかどうかを確認しています。その後、ユーザーがどのくらい回転したかを確認するためにdeltaFactorを使用しました。画像のtopの値を変更するためにその値を使用しました。

また、画像をこの範囲に保つために、minTopmaxTopの値を保持しました。その後、画像がminTopの値に達すると、ページのオーバーフローを有効にして、ページがいつものようにスクロールするようにしました。

$(document).ready(function() { 
 
    var minTop = -200; 
 
    var maxTop = 50; 
 
    $(window).on('mousewheel', function(event) { 
 
    var top = Number($('.imgHolder').css('top').replace('px', '')); 
 
    console.log(top, minTop) 
 
    if (top + event.deltaFactor * (event.deltaY < 0 ? -1 : 1) >= minTop) { 
 
     $('#container').addClass('overflowHidden'); 
 
     top = top + event.deltaFactor * (event.deltaY < 0 ? -1 : 1); 
 
     top = top < minTop ? minTop : top; 
 
     top = top > maxTop ? maxTop : top; 
 
     $('.imgHolder').css('top', top); 
 
    } else { 
 
     $('#container').removeClass('overflowHidden'); 
 
    } 
 
    }); 
 
})
body { 
 
    margin: 0; 
 
} 
 
.content { 
 
    width: 30%; 
 
    margin: auto; 
 
} 
 
.imgHolder { 
 
    width: 80%; 
 
    height: auto; 
 
    position: absolute; 
 
    top: 50px; 
 
    left: 10%; 
 
} 
 
img { 
 
    max-width: 100%; 
 
} 
 
#container.overflowHidden { 
 
    position: absolute; 
 
    width: 100%; 
 
    height: 100%; 
 
    left: 0px; 
 
    top: 0px; 
 
    overflow: hidden; 
 
}
<html> 
 

 
<head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.13/jquery.mousewheel.min.js"></script> 
 
</head> 
 

 
<body> 
 
    <div id="container" class="overflowHidden"> 
 
    <div class="content"> 
 
     On July 1, 1963, Project MAC (the Project on Mathematics and Computation, later backronymed to Multiple Access Computer, Machine Aided Cognitions, or Man and Computer) was launched with a $2 million grant from the Defense Advanced Research Projects Agency 
 
     (DARPA). Project MAC's original director was Robert Fano of MIT's Research Laboratory of Electronics (RLE). Fano decided to call MAC a "project" rather than a "laboratory" for reasons of internal MIT politics—if MAC had been called a laboratory, 
 
     then it would have been more difficult to raid other MIT departments for research staff. The program manager responsible for the DARPA grant was J.C.R. Licklider, who had previously been at MIT conducting research in RLE, and would later succeed 
 
     Fano as director of Project MAC. Project MAC would become famous for groundbreaking research in operating systems, artificial intelligence, and the theory of computation. Its contemporaries included Project Genie at Berkeley, the Stanford Artificial 
 
     Intelligence Laboratory, and (somewhat later) University of Southern California's (USC's) Information Sciences Institute. An "AI Group" including Marvin Minsky (the director), John McCarthy (who invented Lisp) and a talented community of computer 
 
     programmers was incorporated into the newly formed Project MAC. It was interested principally in the problems of vision, mechanical motion and manipulation, and language, which they view as the keys to more intelligent machines. In the 1950s - 1970s 
 
     the AI Group shared a computer room with a computer (initially a PDP-6, and later a PDP-10) for which they built a time-sharing operating system called ITS.[citation needed] The early Project MAC community included Fano, Minsky, Licklider, Fernando 
 
     J. Corbató, and a community of computer programmers and enthusiasts among others who drew their inspiration from former colleague John McCarthy. These founders envisioned the creation of a computer utility whose computational power would be as reliable 
 
     as an electric utility. To this end, Corbató brought the first computer time-sharing system, CTSS, with him from the MIT Computation Center, using the DARPA funding to purchase an IBM 7094 for research use. One of the early focuses of Project MAC 
 
     would be the development of a successor to CTSS, Multics, which was to be the first high availability computer system, developed as a part of an industry consortium including General Electric and Bell Laboratories. In 1966, Scientific American featured 
 
     Project MAC in the September thematic issue devoted to computer science, that was later published in book form. At the time, the system was described as having approximately 100 TTY terminals, mostly on campus but with a few in private homes. Only 
 
     30 users could be logged in at the same time. The project enlisted students in various classes to use the terminals simultaneously in problem solving, simulations, and multi-terminal communications as tests for the multi-access computing software 
 
     being developed.</div> 
 

 
    <div class="imgHolder"> 
 
     <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/Stata_Center1.jpg/800px-Stata_Center1.jpg" /> 
 
    </div> 
 
</body> 
 
</div> 
 

 
</html>

関連する問題