scrolling animation horizontally within DIV is behaving weird - javascript

I'm trying to make a horizontal scroll anchoring inside a DIV. Seems to work if I just animate the whole body tag but doesnt seem to work in a DIV for me.
Heres the javascript
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$(".scroll").click(function(event){
event.preventDefault();
$('#wrapper').animate({scrollLeft:$(this.hash).offset().left}, 500);
});
});
</script>
and heres the Html
<body>
<div id="wrapper">
<div id="wrappercontent">
<div id="section1" class="section">
<img src="images/big_head.png" />
</div>
<div id="section2" class="section">
<img src="images/whatis.png" />
</div>
<div id="section3" class="section">
<h2>Section 3</h2>
<p>
blahblahblabh
</p>
</div>
</div>
<div id="nav">
<li>1</li>
<li>2</li>
<li>3</li>
</div>
</div>
</body>
and heres the stylesheet
*{
margin:0;
padding:0;
}
body{
background:#f0efe4;
letter-spacing:-1px;
font-family:Georgia;
font-size: 34px;
font-style: italic;
width:12000px;
}
#wrapper{
width:1000px;
height:700px;
overflow:scroll;
}
#wrappercontent{
height:auto;
width:12000px;
}
#nav{
z-index:1;
position:fixed;
}
.section{
margin:0px;
bottom:0px;
width:4000px;
float:left;
height:100%;
text-shadow:1px 1px 2px #f0f0f0;
}
Its behaving weirdly. Please help

You just need to account for the current scroll position in your calculation:
scrollLeft: $('#wrapper').scrollLeft() + $(this.hash).offset().left
Demo: http://jsfiddle.net/brianpeiris/h6hvq/

Related

Change the background of nav when scroll

I'm trying to change the background of NAV when i scroll. What i've done already is below. I don't know what else to do!
CSS
html, body{
margin:0;
padding:0;}html{
font-family: Poppins,sans-serif;}.jumbotron{
text-align:center;
color:red;
background-image: url(IMG/dyta.jpg);
background-repeat: no-repeat;
background-attachment: scroll;
background-position: center center;
background-size: cover;
height:100vh;
padding-top:100px;}header{
z-index:1;
position:fixed;
display:flex;
width:100vw;
height:100px;
margin-left:80px;
font-size:20px;
line-height:100px;
color:white;}div.logo{
width:40%;}ul.header-menu-ul{
display:flex;}li.header-menu-li{
list-style:none;
margin-left:auto;
margin-right:auto;}.header-menu-li a{
text-decoration:none;
color:white;
text-transform:uppercase;
letter-spacing:2px;
font-weight: 600;
font-size:14px;
font-family: Poppins,sans-serif;}.header-nav.scrolled {
background-color: #fff;
transition: background-color 200ms linear;}
HTML
<header class="container-fluid header-nav">
<div class="logo text-center">
<i class="fab fa-google"></i>
</div>
<nav class="container-fluid nav-items text-center">
<ul class="header-menu-ul nav-links">
<li class="header-menu-li"><a title="AboutUs" href="#">About Us</a></li>
<li class="header-menu-li"><a title="Services" href="#">Services</a></li>
<li class="header-menu-li"><a title="OurProjects" href="#">Our Projects</a></li>
<li class="header-menu-li"><a title="News" href="#">News</a></li>
<li class="header-menu-li"><a title="Contact" href="#">Contact</a></li>
</ul>
</nav></header>
<div class="jumbotron section text-center">
<h1>Company</h1>
<p>We specialize in blablabla</p>
</div>
JavaScript
$(function () {$(document).scroll(function () {
var $nav = $(".header-nav");
$nav.toggleClass('scrolled', $(this).scrollTop() > $nav.height());
});});
I've been stuck in this for 3 days and i don't know what else to do! I don't kno where my problem is.Can anyone please help me??
try this
$(function () {
$(document).scroll(function () {
if($(this).scrollTop() > $(".header-nav").height()) {
$(".header-nav").addClass('scrolled');
}
});
});
make sum height for your content to see your code works, see this: https://codepen.io/anon/pen/bzbOzv
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>

Shift menu vertically and display sub menu

I am trying to write one vertical menu like in this website . On hover the vertical menu should shift marginally to the right side and should display the sub menu.
I have been failed to accomplish this.
Please help me :(
<html>
<head>
<title></title>
<style>
.apear
{
width:200px;
height:300px;
background-color:pink;
float:right;
display:none;
}
.one
{
background-color:yellow;
height:100px;
width:100px
}
.two
{
background-color:blue;
height:100px;
width:100px
}
.three
{
background-color:green;
height:100px;
width:100px
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<div>
<div class="apear">
</div>
<div style="float:right">
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
</div>
</div>
</body>
</html>
Here's the solution, I used JQuery:
HTML:
<div>
<div class="apear">
</div>
<div style="float:right">
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
</div>
CSS:
.apear
{
width:200px;
height:300px;
background-color:pink;
float:right;
display:none;
}
.one
{
background-color:yellow;
height:100px;
width:100px
}
.two
{
background-color:blue;
height:100px;
width:100px
}
.three
{
background-color:green;
height:100px;
width:100px
}
JQuery:
$(document).ready(function(){
$(".one").mouseenter(function(){
$(".apear").show(500);
});
$(".apear").mouseleave(function(){
$(".apear").hide(500);
});
});
Here is a fiddle

How to change styles of several child divs of a div?

I'm making a navigation and I don't want to use CSS3 to make gradient background instead of that i use images. But buttons has corners so my button is made from 3 images to make its size proportional to text. I am trying to change background on hover and I need to change all 3 backgrounds: left, right and mid. I tried jquery but it doesn't seem to work..
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(function(){
$('.topnavbtn').mouseenter(function(){
$(this).$('.left').css('background','url(images/nav/images/navLeft_on.png) no-repeat');
$(this).$('.mid').css('background','url(images/nav/images/navBg_on.png) no-repeat');
$(this).$('.right').css('background','url(images/nav/images/navRight_on.png) no-repeat');
}).mouseout(function(){
$(this).$('.left').css('background','url(images/nav/images/navLeft.png) no-repeat');
$(this).$('.mid').css('background','url(images/nav/images/navBg.png) no-repeat');
$(this).$('.right').css('background','url(images/nav/images/navRight.png) no-repeat');
});
});
</script>
HTML:
<ul style="list-style-type: none; margin:0; padding:0;padding:10px;">
<li>
<a href="#">
<div class="topnavbtn">
<div class="left"></div>
<div class="mid">asdassdfdssdfsd</div>
<div class="right"></div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</a>
</li>
<li>
<a href="#">
<div class="topnavbtn">
<div class="left"></div>
<div class="mid">asdassdfdssdfsd</div>
<div class="right"></div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</a>
</li>
<li>
<a href="#">
<div class="topnavbtn">
<div class="left"></div>
<div class="mid">asdassdfdssdfsd</div>
<div class="right"></div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</a>
</li>
</ul>
And CSS:
.topnavbtn {
height:34px;
float:left;
display:inline-block;
margin:7px;
font-weight:bold;
text-transform:uppercase;
color:#FFF;
line-height: 34px;
}
.topnavbtn .left {
height:34px;
width:9px;
background:url(images/nav/images/navLeft.png) no-repeat;
float:left;
display:inline-block;
}
.topnavbtn .right {
height:34px;
width:9px;
background:url(images/nav/images/navRight.png) no-repeat;
float:left;
display:inline-block;
}
.topnavbtn .mid {
background:url(images/nav/images/navBg.png) repeat-x;
height:34px;
width:auto;
float:left;
display:inline-block;
}
This is a syntax error :
$(this).$('.left').css(...
try
$('.left', this).css(...
Just replace
$(this).$('.left')
with
$(this).children('.left')
and so on for rest of classes .

javascript image pan 'blocking' nav links and making div elements opaque

I have an image, bg, embedded in a div using CSS just above the footer. The idea is when the mouse moves left or right, the image moves along with it. The Javascript above /body is what causes this to work. It works well, but the nav links are now unclickable and the content divs are opaque where you can see the bg image through them. When I remove the #bg div, everything works fine. Any ideas how to fix it? HTML and CSS are below. I got this from tutorial here: panning.
<!DOCTYPE HTML>
<html>
<head>
<title>portfolio</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<link rel="shortcut icon" href="img/favicon.ico">
<link rel="stylesheet" type="text/css" href="css/reset.css">
<link rel="stylesheet" type="text/css" href="css/web.css">
<script src="js/modernizr.custom.js"></script>
<script src="js/modernizr-2.6.2.min.js"></script>
</head>
<body>
<div id="site">
<header>
<nav>
<ul>
<li>HOME</li>
<li><img src="img/lightning.png" alt="lightning" width="10" height="16"></li>
<li>PORTFOLIO</li>
<li><img src="img/lightning.png" alt="lightning" width="10" height="16"></li>
<li>CODE</li>
<li><img src="img/lightning.png" alt="lightning" width="10" height="16"></li>
<li>CONTACT</li>
</ul>
</nav>
</header>
<div id="left_col1">
<a href="../photo_collection_blom/index.html" target="blank"><img src="img/10.png" alt="image" width="280"
height="170" class="fade"></a>
<p>lorem ipsum</p>
</div>
<div id="mid_col1">
<a href="../non_profit/index.html" target="blank"><img src="img/vpf.png" alt="image" width="280" height="170"
class="fade"></a>
<p>lorem ipsum</p>
</div>
<div id="right_col1">
<a href="../cd/index.html" target="blank"><img src="img/cd.png" alt="image" width="280" height="170"
class="fade"></a>
<p>lorem ipsum</p>
</div>
<div class="clearfix"></div>
<div id="left_col2">
<img src="img/zen.png" alt="image" width="280" height="170">
<p>lorem ipsum</p>
</div>
<div id="mid_col2">
<a href="../practicum/index.html" target="blank"><img src="img/practicum.png" alt="image" width="280"
height="170"></a>
<p>lorem ipsum</p>
</div>
<div id="right_col2">
<a href="../dropdown_nav/index.html" target="blank"><img src="img/navs.png" alt="image" width="280"
height="170"></a>
<p>lorem ipsum</p>
</div>
<div class="clearfix"></div>
<div id="left_col3">
<a href="http://centralpaintstore.com" target="blank"><img src="img/cps.png" alt="image" width="280"
height="170"></a>
<p>lorem ipsum</p>
</div>
<div id="mid_col3">
<img src="img/88.png" alt="image" width="280" height="170">
<p>lorem ipsum</p>
</div>
<div id="right_col3">
<img src="img/99.png" alt="image" width="280" height="170">
<p>lorem ipsum</p>
</div>
<div class="clearfix"></div>
<div id="bg"></div>
<footer>
<p>lorem ipsum</p>
</footer>
</div>
<!-- close site -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/jquery-1.9.1.min.js"><\/script>')</script>
<script>
$(document).ready(function () {
$('#bg').mousemove(function (e) {
var mousePos = (e.pageX / $(window).width()) * 100;
$('#bg').css('backgroundPosition', mousePos + '% 0');
});
});
</script>
</body>
</html>
CSS:
* {
text-decoration:none;
font-family:Helvetica, sans-serif;
z-index:100;
}
html {
background:url(../img/background.gif);
height:100%;
}
body {
height:100%;
z-index:100;
}
#site {
width:1200px;
min-height:100%;
margin:auto;
position:relative;
z-index:100;
}
header {
background:url(../img/background_design.jpg);
height:63px;
padding-top:25px;
z-index:100;
}
nav {
width:500px;
padding:10px;
margin:auto;
font-family:helvetica, arial, sans-serif;
font-size:1.2em;
color:#dcd8cf;
z-index:100;
}
nav ul {
word-spacing:20px;
}
li {
display:inline;
margin:auto;
}
a:link {
color:#dcd8cf;
}
/* unvisited link */
a:visited {
color:#e25d33;
}
/* visited link */
a:hover {
color:#e25d33;
}
/* mouse over link */
a:active {
color:#e25d33;
}
/* selected link */
p {
font-family:Arial, Helvetica, sans-serif;
color:#000;
line-height:1.2em;
margin:10px 5px 20px 5px;
}
#site p a:link {
color:#ed1b24;
}
#site p a:visited {
color:#ed1b24;
}
#site p a:hover {
text-decoration:underline;
}
#site p a:active {
color:#ed1b24;
}
h3 {
font-family:helvetica, arial, sans-serif;
font-size:1.5em;
color:#000;
margin:20px;
}
#bg {
background:url(../img/boston_skyline4.png) no-repeat 0 0 scroll;
height:auto;
left:0;
min-height:100%;
min-width:1024px;
overflow:hidden;
position:fixed;
top:0;
width:100%;
z-index:1;
}
#left_col1, #left_col2, #left_col3 {
border:1px solid #e0dfdf;
width:280px;
height:384px;
margin-left:120px;
margin-right:20px;
background:white;
padding:10px;
overflow-x:scroll;
float:left;
z-index:100;
}
.clearfix {
height:20px;
clear:both;
}
#mid_col1, #mid_col2, #mid_col3 {
border:1px solid #e0dfdf;
width:280px;
height:384px;
margin-right:20px;
background:white;
padding:10px;
overflow-x:scroll;
float:left;
z-index:100;
}
#right_col1, #right_col2, #right_col3 {
border:1px solid #e0dfdf;
width:280px;
height:384px;
background:white;
padding:10px;
overflow-x:scroll;
float:left;
z-index:100;
}
#left_col1, #mid_col1, #right_col1 {
margin-top:20px;
}
#left_col3, #mid_col3, #right_col3 {
margin-bottom:20px;
}
footer {
background:url(../img/footer.gif);
margin:auto;
position:absolute;
bottom:0;
width:1200px;
height:55px;
}
footer p {
font-family:helvetica, arial, sans-serif;
color:#e0dfdf;
font-size:.9em;
margin:auto;
padding-top:1.5em;
padding-left:1.5em;
}
The <div id="bg"> tag is sitting on top of everything else in the <div id="body" wrapper. Z-index won't work on your floated columns, so you'll have to wrap your columns in another div or move the <div id="bg"> tag outside of the <div id="body" tag.
Here's a fiddle of the first solution: http://jsfiddle.net/hjeAT/3/
And the second: http://jsfiddle.net/hjeAT/4/
i think the z-index of <div id="bg"> changed when run. you should check the z-index when the nav links are unclickable. may be you can change the z-index in your javascript. i have another advise that you'd better remove the z-index in the css of the * selector as follows
* {
text-decoration:none;
font-family:Helvetica, sans-serif;
z-index:100;
}
Here is one solution:
Just delete <div id="bg"></div> and
CSS:
body {
height:100%;
background:url(../img/boston_skyline4.jpg) no-repeat 0 0 scroll;
}
script:
You can change the mousemove event target.
<script>
$(document).ready(function () {
$('body').mousemove(function (e) {
var mousePos = (e.pageX / $(window).width()) * 100;
$('body').css('backgroundPosition', mousePos + '% 0');
});
});
</script>

Javascript slider doesn't work locally?

okay guys, I found a small piece of code on stackoverflow which is for a slidehsow using javascript. But for some reason it doesn't work on my PC. This is the original Post:
Looking for a fullwidth slider
it works there but for some reason it doesn't work for me!!
what am I doing wrong?
Here is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.clear { clear:both; }
.wrapper { width:980px; margin:0 auto; }
.slider { margin:20px 0; height:100px; position:relative; }
.slider .slide { display:none; background:red; position:absolute; height:100px; width:100%; text-align:center; color:#fff; font-size:24pt; }
.header { background:#eee; font-size:18pt; }
.content { }
.footer { background:#eee; text-align:center; }
.slider-nav { margin: 0 auto; width:100px; clear:both; }
.slider-nav li { float:left; margin:0 5px; }
-->
</style>
<script type="text/javascript">
$('.slider .slide:first').addClass('active').fadeIn(200);
function rotate(index) {
$('.slider .slide.active').removeClass('active').fadeOut(200, function() {
$('.slider .slide:eq(' + index + ')').addClass('active').fadeIn(200);
});
}
$('.slider-nav li a').click(function() {
var index = $(this).parent().index('li');
rotate(index);
return false;
});
setInterval(function() {
var $next = $('.slider .slide.active').next();
if ($next.length == 0)
$next = $('.slider .slide:first');
rotate($next.index());
}, 2000);
</script>
</head>
<body>
<div class="header wrapper">
<h1>Site header</h1>
</div>
<div class="slider">
<div class="slide">1</div>
<div class="slide">2</div>
<div class="slide">3</div>
<div class="slide">4</div>
</div>
<ul class="slider-nav">
<li><a href="#" >1</a></li>
<li><a href="#" >2</a></li>
<li><a href="#" >3</a></li>
<li><a href="#" >4</a></li>
</ul>
<div class="clear"></div>
<div class="content">
<div class="wrapper">
<p>Some site content will be here</p>
<p>Some site content will be here</p>
<p>Some site content will be here</p>
<p>Some site content will be here</p>
<p>Some site content will be here</p>
</div>
</div>
<div class="footer">
<div class="wrapper">© www.mysite.com </div>
</div>
</body>
</html>
You seem to reference a javascript library (maybe jquery) in your code, but no trace of the library inclusion.
You are using jQuery code but where is your reference to the jQuery library?

Categories