I am entirely new to Javascript, usually working just as a designer in CSS / HTML (And the occasional cut/paste piece of pre-written JS) but couldn't see anyway other than JS to achieve what I am after so had a go.... but failed!
What I want to achieve is a parent that contains a much larger child whereby moving the mouse to the edge of the parent will scroll it in that direction (Think first person video games type effect) with the ultimate goal being to give the effect of being stood in the middle of a room and looking around. (Currently child is just gradient filled to test and keep code short until I get it working)
After days of googling every combination of words I could think of to find this and not finding exactly what I want, I have found the closest answers I could on stackoverflow and pieced it together as best as I could.... but (with grim predictability) it doesn't work!
So what I am looking for is anything ranging from somebody giving me the code I need to hints or pointers towards what I have done wrong. My code as it stands in all below along with a link to Codepen showing it not working :(
Bonus Question: Once I have got this working my next goal was to make it so if you scrolled all the way either left or right it jumped to the opposite end of the (Eg scroll right > 100% takes you to very left of div) so once related back to being in the room you can turn 360^o and then keep going... Haven't even started looking at this yet, but thought I'd through it out now in case a) it would be bound up in the script for the first part, or b) it simply isn't possible and you could save me a lot of time trying to look up how to do it.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>TEST</title>
<style media="screen" type="text/css">
html{
position:relative;
font-size:62.5%;
margin:0;
padding:0;
width:100%;
}
body{
position:relative;
margin:0;
padding:0;
height:100%;
width:100%;
overflow:hidden;
background:blue;
}
#container{
position:relative;
box-sizing:border-box;
width:calc(100vw - 4rem);
height:calc(100vh - 4rem);
overflow: hidden;
margin-left:2rem;
margin-top:2rem;
}
#content{
width:200vw;
height:200vh;
background: #ff0000;
background: -moz-linear-gradient(-45deg, #ff0000 0%, #00ff00 50%, #0000ff 100%);
background: -webkit-linear-gradient(-45deg, #ff0000 0%,#00ff00 50%,#0000ff 100%);
background: linear-gradient(135deg, #ff0000 0%,#00ff00 50%,#0000ff 100%);
}
#hoverleft{
position:absolute;
cursor:pointer;
left:0;
top:0;
width:5rem;
height:100%;
background:rgba(0,0,0,0.5);
-webkit-clip-path: polygon(0 0, 100% 5rem, 100% calc(100% - 5rem), 0% 100%);
clip-path: polygon(0 0, 100% 5rem, 100% calc(100% - 5rem), 0% 100%);
}
#hoverright{
position:absolute;
cursor:pointer;
right:0;
top:0;
width:5rem;
height:100%;
background:rgba(0,0,0,0.5);
-webkit-clip-path: polygon(0 5rem, 100% 0, 100% 100%, 0 calc(100% - 5rem));
clip-path: polygon(0 5rem, 100% 0, 100% 100%, 0 calc(100% - 5rem));
}
#hoverup{
position:absolute;
cursor:pointer;
left:0;
top:0;
width:100%;
height:5rem;
background:rgba(250,250,250,0.5);
-webkit-clip-path: polygon(0 0, 100% 0, calc(100% - 5rem) 100%, 5rem 100%);
clip-path: polygon(0 0, 100% 0, calc(100% - 5rem) 100%, 5rem 100%);
}
#hoverdown{
position:absolute;
cursor:pointer;
left:0;
bottom:0;
width:100%;
height:5rem;
background:rgba(250,250,250,0.5);
-webkit-clip-path: polygon(5rem 0, calc(100% - 5rem) 0, 100% 100%, 0 100%);
clip-path: polygon(5rem 0, calc(100% - 5rem) 0, 100% 100%, 0 100%);
}
</style>
<script type="text/javascript">
var amount = '';
function scroll() {
$('#container').animate({
scrollTop: amount
}, 100, 'linear',function() {
if (amount != '') {
scroll();
}
});
}
$('#hoverup').hover(function() {
amount = '+=10';
scroll();
}, function() {
amount = '';
});
$('#hoverdown').hover(function() {
amount = '-=10';
scroll();
}, function() {
amount = '';
});
var amount = '';
function scroll() {
$('#container').animate({
scrollLeft: amount
}, 100, 'linear',function() {
if (amount != '') {
scroll();
}
});
}
$('#hoverleft').hover(function() {
amount = '+=10';
scroll();
}, function() {
amount = '';
});
$('#hoverright').hover(function() {
amount = '-=10';
scroll();
}, function() {
amount = '';
});
</script>
</head>
<body>
<div id="container">
<div id="content">
</div>
<div id="hoverleft"></div>
<div id="hoverright"></div>
<div id="hoverup"></div>
<div id="hoverdown"></div>
</div>
</body>
</html>