javascript horizontal scroll with buttons - javascript

On mu profile pages I have a small div where I display awards for doing different things. to save space the div is really small but I was gone have a horizontal scroll using buttons.
<div class="award-wrapper fr" style="width:19%; margin:auto;">
<h3 class="award-title"><i class="fa fa-trophy"></i> Awards</h3>
<div class="award-content" id="inner outer" style=" margin:0 auto; text-align:center;">
<div style="float:left; height:100%; left:0; position:absolute;">
<input type="button" value="«" class="scroll_button" id="left-button"/>
</div>
<div id="myDiv" style="float: left;">
<?php include"award.php"; ?>
</div>
<div style="float:right; height:100%; right:0; position:absolute;">
<input type="button" value="»" class="scroll_button" id="right-button"/>
</div>
</div>
</div>
.scroll_button {
opacity: 0.5;
color:#fff;
padding:3px;
height:100%;
}
.scroll_button:hover {
opacity: 0.8;
color:#05c7f7;
}
.scroll_button:active {
color:#05c7f7;
opacity: 0.8;
background:#08090a;
}
not sure if any other parts of my code is necessary

If you mean having a left button to force the scrollbar left, and a similiar right to force it right, it's definitely so plausible.
https://jsfiddle.net/5ug583ff/
var button = document.getElementById('slide');
button.onclick = function () {
document.getElementById('container').scrollLeft += 20;
};
Code example found here: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollLeft
I am not 100% sure if this is the answer to your question as I didn't fully understand your question.

If you want to allow horizontal scrolling in your awards div...
Replace this:
<div id="myDiv" style="float: left;">
<?php include"award.php"; ?>
</div>
With this:
<div id="myDiv" style="float: left; overflow-x:scroll;">
<?php include"award.php"; ?>
</div>

Related

How to Stick elements on page scroll

I have a one page, scrolling site with 5 main sections that have title bars that span across the top of each respective section. I want each title bar to stick at the top (well, relative top-underneath the top sticky header) as you scroll down the section. I can get one to stick, but I am having trouble making it so that one sticks and then it goes away once the next section's title bar gets to the sticky point.
I can't figure out another way to bind the HTML or CSS with the jQuery if else statement to make this work. I was thinking I could try to make it work within each sections' id but I don't think there's like a "withinId" jQuery selector.
I'm posting the latest jQuery I attempted (with just 2 out of the 5 variables I will need to make work here). I know it's wrong but I'm seriously stuck. Any ideas here? Thanks a million.
(abbreviated) HTML:
<div id="welcome">
<div class="title-bar">
<p>WELCOME</p>
</div>
</div>
<div id="global">
<div class="title-bar">
<p>GLOBAL ENGAGEMENT</p>
</div>
</div>
<div id="community">
<div class="title-bar">
<p>COMMUNITY</p>
</div>
</div>
<div id="resources">
<div class="title-bar">
<p>RESOURCES</p>
</div>
</div>
<div id="horizon">
<div class="title-bar">
<p>ON THE HORIZON</p>
</div>
</div>
CSS:
.title-bar {
padding: 5px;
position: relative;
}
.title-bar.sticky {
position: fixed;
top: 111px;
width: 100%;
z-index: 1040;
}
jQuery:
$(document).ready(function() {
var welcomeTitle = $('#welcome .title-bar');
var globalTitle = $('#global .title-bar');
var communityTitle = $('#community .title-bar');
var resourcesTitle = $('#resources .title-bar');
var horizonTitle = $('#horizon .title-bar');
var stickyOffset = $('#header').offset().top;
if ($w.scrollTop() > stickyOffset + 225) {
welcomeTitle.addClass('sticky');
globalTitle.addClass('sticky');
} else {
welcomeTitle.removeClass('sticky');
globalTitle.addClass('sticky');
}
if (welcomeTitle.hasClass('sticky') && globalTitle.hasClass('sticky')) {
welcomeTitle.removeClass('sticky');
} else {
//
}
});
jsBin demo
Give your "pages" a class="page" and listen for their positions using JS's Element.getBoundingClientRect on: DOM Ready, window Load, window Scroll
$(function() { // DOM ready
var $win = $(window),
$page = $(".page").each(function(){
// Memorize their titles elements (performance boost)
this._bar = $(this).find(".title-bar");
});
function fixpos() {
$page.each(function(){
var br = this.getBoundingClientRect();
$(this._bar).toggleClass("sticky", br.top<0 && br.bottom>0);
});
}
fixpos(); // on DOM ready
$win.on("load scroll", fixpos); // and load + scroll
});
*{box-sizing: border-box;}
html, body{height:100%;}
body{margin:0;font:16px/1 sans-serif; color:#777;}
.page{
position:relative;
min-height:100vh;
}
.title-bar {
position: absolute;
top:0;
width: 100%;
background:#fff;
box-shadow: 0 3px 4px rgba(0,0,0,0.3);
}
.title-bar.sticky {
position: fixed;
}
#welcome {background:#5fc;}
#global {background:#f5c;}
#community{background:#cf5;}
#resources{background:#fc5;}
#horizon {background:#5cf;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="welcome" class="page">
<div class="title-bar">
<h2>WELCOME</h2>
</div>
</div>
<div id="global" class="page">
<div class="title-bar">
<h2>GLOBAL ENGAGEMENT</h2>
</div>
</div>
<div id="community" class="page">
<div class="title-bar">
<h2>COMMUNITY</h2>
</div>
</div>
<div id="resources" class="page">
<div class="title-bar">
<h2>RESOURCES</h2>
</div>
</div>
<div id="horizon" class="page">
<div class="title-bar">
<h2>ON THE HORIZON</h2>
</div>
</div>
Design-wise > add a padding-top to the first container element (inside your .page) to prevent content going underneath the title element (since it toggles from absolute/fixed positions).
Have a look at the Waypoints plugin.
You can probably make it a little easier on yourself by assigning each section a class and then add and remove the class from each section with jquery each function.
Try something like the following:
$(window).on( "scroll", function() {
$( ".section" ).each(function() {
if ( $(window).scrollTop() >= $(this).offset().top - 50 ) {
$( this ).addClass("sticky");
}else{
$( this ).removeClass("sticky");
}
});
});
Then your css
.section{
height: 200px;
background: #333;
border:1px solid #222;
position:relative;
}
.section .title-bar{
position:absolute;
top:0;
left:0;
width:100%;
height:50px;
}
.section.sticky .title-bar {
position:fixed;
}
And html
<div class="section">
<div class="title-bar"></div>
</div>
<div class="section">
<div class="title-bar"></div>
</div>
<div class="section">
<div class="title-bar"></div>
</div>

Setting div to rest of available space

I have a parent div and 2 child divs (child-left and child-right). child-right div will contain 1 or 2 icons depending on dynamic page requirement. The child-left div contains the title and also used as a handle to drag operation. I do not want to set a width px or % (like the 90% I have below). How do I set the child-left div to take the rest of available space after what is occupied by child-right.
<div id="parent">
<div id="child-left" style="width:90%">
This is my title
</div>
<div id="child-right">
<i class="fa fa-cog"></i>
</div>
</div>
Thanks!
The following should help you:
HTML:
<div id="parent">
<div id="child-right">Hey</div>
<div id="child-left">This is my title</div>
</div>
CSS:
#child-left {
border: 3px solid gray;
background-color:blue;
margin-left:0px;
overflow:hidden;
}
#child-right {
float:right;
border-style:solid;
}
#parent {
overflow:hidden;
}
DEMO JSFiddle

CTRL+F moves overflow:hidden <div>

I am trying to create a slider but discovered that if a user were to use CTRL+F, the position and the <div> element's offset changed and so the slider no longer works the way is should.
HTML:
<div style="width:100px; height:150px;">
<div style="width:100px; height:100px; overflow:hidden;">
<div id="slider" style="width:200px; height:100px; right:0; position:relative;">
<div style="width:100px; height:100px; float:left;">visible</div>
<div style="width:100px; height:100px; float:left;">hidden</div>
</div>
</div>
<input id="sliderbuttonprev" type="button" style="float:left;" value="Prev">
<input id="sliderbuttonnext" type="button" style="float:right;" value="Next">
</div>
JavaScript (jQuery):
$(document).ready(function() {
$("#sliderbuttonnext").click(function(){
$("#slider").animate({right:"+=100px"});
});
$("#sliderbuttonprev").click(function(){
$("#slider").animate({right:"-=100px"});
});
});
Is there a way to stop CTRL+ F finding the hidden sections?
jsFiddle Demo
You cannot prevent browsers from finding hidden content, but you could potentially disable it for the slides.
For example, if you specify the content within CSS, the browser won't move the content. For example, see here > Is it possible to disable Ctrl + F of find in page?
<div class="word-foobar"></div>
.word-foobar:before {
content: "Foobar";
}
As nickf has suggested, you could easily write some JavaScript code to convert actual text to this method.
http://jsfiddle.net/TaZL2/2/
If you change your animation to marginLeft instead of the right property, the content doesn't seem to scroll when searching. (Chrome/Mac OSX)
However, a user would still see there was a match and be stumped as to where it could be.
$("#sliderbuttonnext").click(function () {
$("#slider").animate({
marginLeft: "-=100px"
});
});
$("#sliderbuttonprev").click(function () {
$("#slider").animate({
marginLeft: "+=100px"
});
});
i came up with a solution that uses a variable to track the position of the main wrapping div and hides ".hide()" the content div that's not visible. hidden content is not visible to ctrl f.
HTML:
<div style="width:100px; height:150px;">
<div style="width:100px; height:100px; overflow:hidden;">
<div id="slider" style="width:200px; height:100px; right:0; position:relative;">
<div style="width:100px; height:100px; float:left;">
<div id="id1">visible</div>
</div>
<div style="width:100px; height:100px; float:left;">
<div id="id2">hidden</div>
</div>
</div>
</div>
<input id="sliderbuttonprev" type="button" style="float:left;" value="Prev">
<input id="sliderbuttonnext" type="button" style="float:right;" value="Next">
JQuery
<script>
var pos = 0;
function showfunct(x){
if(x==0)$("#id1").show();
if(x==100)$("#id2").show();
}
function hidefunct(x){
if(!(x==0))$("#id1").hide();
if(!(x==100))$("#id2").hide();
}
showfunct(pos);
hidefunct(pos);
$(document).ready(function() {
$("#sliderbuttonnext").click(function(){
pos+=100;
showfunct(pos);
$("#slider").animate({right:"+=100px"});
$("#slider").promise().done(function(){
hidefunct(pos);
});
});
$("#sliderbuttonprev").click(function(){
pos-=100;
showfunct(pos);
$("#slider").animate({right:"-=100px"});
$("#slider").promise().done(function(){
hidefunct(pos);
});
});
});
</script>

HTML positioning of divs above image

I have a large image, and a bunch of divs that I'm using as fake buttons on top of said image. Currently, I'm using absolute positioning to place the divs where I want them to be, but I've got a lot of divs, and finding the x/y coords by trial and error is time I don't want to take. Is there an easier way to position them, or am I stuck?
I'm using jQuery and Javascript in this project, so these can be used for solutions.
CSS:
#test0 {
position:absolute;
left:381px;
bottom:100px;
}
HTML:
<div id="image">
<div id="test0" class="button" onclick="$('#modal').dialog('open');" style="postion:absolute">
Click me to test Modal!
</div>
<div id="test1" class="button" onclick="$('#modal').dialog('open');" style="postion:absolute">
Click me to test the same Modal!
</div>
<img src="testImage.jpg" alt="testtest" />
</div>
HTML:
<div id="image">
<div id="container-of-fake-divs">
<div class="fake-div">FAKE DIV</div>
<div class="fake-div">FAKE DIV</div>
</div>
<img src="image.jpg" />
</div>
STYLE:
#image { position:relative; }
#container-of-fake-divs { position:absolute; top:0; left:0; }
.fake-div { display:block; }

HTML/CSS - Displaying text over an img tag

I'm trying to display two images side to side, with text, controls and whatever else my heart desires over them.
To do this, I have the following:
<div>
<div id="leftDiv" style="float:left; width:49%; height:400px; background-color:transparent">
<div style="position:relative; left:61px; top:100px; width: 319px; z-index:1">This is text</div>
<div style="position:relative; left:61px; top:100px; width: 319px; z-index:1">This is also text</div>
<img id="leftImg" alt="Images/redbox.png"
style="width:100%; height:100%; z-index:-1; right: 1124px; top: 9px;"
src="Images/redbox.png" />
</div>
<div id="rightDiv" style="float:left; width:49%; height:400px; background-color:transparent">
<img id="rightImg" src="Images/bluebox.png" alt="Images/bluebox.png" style="width:100%;height:100%; z-index:-1;" />
</div>
</div>
This is all great except for one little thing... The left div, the "redbox.png" is always scooted down by the number of s I want inside it (or any place taken by the elements).
I could place the elements after the image, but it's really easier to place them where I want this way, and to keep them in place when I animate the boxes.
Now, why am I using images instead of background-img? Well I want the images to resize to the surrounding <div>s automatically, and this is the only way I found of doing it easily (resizing manually with javascript is an option, but a complicated one at that, since the boxes will be animated).
Any ideas? Thanks!
You should use position:absolute rather than position:relative in order to take the element out of flow. You will need to adjust the left and top attributes, however.
http://jsfiddle.net/3fJcR/1/
<div>
<div id="leftDiv" style="float:left; width:49%; height:400px; background-color:transparent; position: relative">
<div style="position:absolute; left:61px; top:50px; width: 319px; z-index:1">This is text</div>
<div style="position:absolute; left:61px; top:64px; width: 319px; z-index:1">This is also text</div>
<img id="leftImg" alt="Images/redbox.png"
style="width:100%; height:100%; z-index:-1; right: 1124px; top: 9px;"
src="Images/redbox.png" />
</div>
<div id="rightDiv" style="float:left; width:49%; height:400px; background-color:transparent; position: relative">
<img id="rightImg" src="Images/bluebox.png" alt="Images/bluebox.png" style="width:100%;height:100%; z-index:-1;" />
</div>
</div>
You can have position:absolute for your wrapping div and position:absolute for inner overlay too. Then the inner absolute is relative to outer absolute positioned element not the body.
Look at this example to see what I'm saying:
http://jsfiddle.net/mohsen/TbkjK/7/
For writing text over image you put image in background style and alt text like this-
<img scr="" alt="text"/>
<style>
.img{background-image:url('IMAGE_URL'); }
</style>

Categories

Resources