Having trouble making this product slider - javascript

So,i want to make a product viewer kind of a thing having thumbnails which can slide back or front by clicking on the back/front buttons. I dont know why my code isn't working.Heres the code. Please help me out finding issues in it.Thanks!
Html code
<div id="wrapper">
<div id="main-image">
</div>
<div class='main-slider'>
<div class="window">
<div class='slider-large-image'>
<img src='img3.png' height="500" width="960"> </img>
<img src='img2.png' height="500" width="960"> </img>
<img src='img3.png' height="500" width="960"> </img>
<img src='img4.png' height="500" width="960"> </img>
</div>
</div>
<div class='slider-pager'>
‹
›
</div>
</div>
Javascript code
$(document).ready(function() {
var imagewidth = /*$(".window").width()*/960;
var imagesum = /*$(".slider-large-image img").size()*/5;
var imagereelwidth = imagewidth * imagesum;
$(".slider-large-image").css({'width' : imagereelwidth});
rotatef = function() {
$(".slider-large-image").animate({
left : -imagewidth
},500 );
};
rotateb = function() {
$(".slider-large-image").animate({
left : imagewidth
},500 );
};
$(".slider-pager a#b").click(function() {
rotateb(); //Trigger rotation immediately
//return false; //Prevent browser jump to link anchor
});
$(".slider-pager a#f").click(function() {
rotatef(); //Trigger rotation immediately
//return false; //Prevent browser jump to link anchor
});
});
CSS
#wrapper
{
margin:0 auto;
}
.main-slider
{
float:left;
position:relative;
margin-bottom:10px;
/*background-color:#CCC;*/
border: 0px solid #000;
top:25px;
left:0px;
z-index:1004;
-moz-border-radius:5px;
border-radius:5px;
-moz-box-shadow: 0px 0px 30px 1px #999;
-webkit-box-shadow: 0px 0px 30px 1px #999;
box-shadow: 0px 0px 30px 1px #999;
}
.window
{
width: 960px;
height: 500px;
overflow:hidden;
position:relative;
}
.slider-large-image
{
position:absolute;
top:0px;
left:0px;
}
.slider-large-image img {float:left;}
.slider-pager
{
position:absolute;
float:left;
width: 100px;
height: 10px;
background-color:#333;
top:0%;
left:89.5%;
padding-bottom:10px;
padding-right:0;
}
.slider-pager a
{
padding:1px;
text-align:center;
text-decoration:none;
font-size:20px;
font-weight:700;
color:#ccc;
margin-right:5px;
width:1px;
height:1px;
position:relative;
top:-10px;
}
.main-slider
{
padding:0px;
color:#FFF;
text-align: center;
line-height: 40px;
font:"Comic Sans MS", cursive;
font-size:20px;
text-decoration:none;
}
.main-slider .slider-pager a:hover
{
background-color:#999;
-moz-border-radius:10px;
border-radius:10px;
color:black;
}
.main-slider .slider-pager a.active
{
background-color:#999;
-moz-border-radius:10px;
border-radius:10px;
}
.main-slider .info-page
{
background-color:#000;
width:600px;
height:50px;
text-align:center;
text-shadow:#666;
font:"28 Days Later";
color:#FFF;
line-height: 40px;
font-size:40px
}
.main-slider .info-page #d:hover
{
color:#FF0;
}

Lets start off with your formatting (that I fixed, btw):
Your first <div id="wrapper"> doesn't get closed!
IMG-tag is self ending! WRONG: <img src=""></img> CORRECT: <img src="" />
Some of your css3-variables are for future browsers and -moz-, but is missing -webkit-!! Example: -moz-border-radius: 10px; border-radius: 10px;
Also, at one point you're using ='' and another ="". Its basically okay, but gets annoying to maintain later and doesn't look very clean! Example: <img src='img3.png' height="200" width="300" />
I did not find any function for <div id="main-image"></div> in your provided code. Next time remove these type of elements when your putting together a question here, as this element is 100% irrelevant to the problem at hand. (Same goes for
and .main-slider .info-page #d:hover {} in your css)
Didn't see any reason, why .main-slider {} should be declared twice in the css (so I merged them, if you do have special purpose, then put it back like it was)
If you are not building some fallback version for some other browser version (like mobile version for example.) Then there is no need to put width="" and height="" variables to your IMG-tag direct. You already had a correct place in css for that: .slider-large-image img {float: left; width:; height:;}
In your css, you have .slider-pager a and .main-slider .slider-pager a:hover. Why add .main-slider in front of .slider-pager a:hover anyway?
If you have same parameters for same variable in css, then use comma to merge them: .slider-pager a:hover, .slider-pager a:active {background-color: #999; -moz-border-radius: 10px; border-radius: 10px;}
Your using position: absolute; too much. I strongly believe, you were meaning to use position: relative;
There was an extra }); at the end of it all. Was the code originally in some function or a plugin?
No such parameter as font, in css!
NOTES:
You didn't provide the images. Next time please google some images, so it would be quicker to help you.
Use sites like jsfiddle.net, to make good examples of your problem. You might be asking: Why should I waste my time on that? We will ask the same question from you, when we are looking at your examples. This will REALLY make things faster for you are for us, on finding quick and valuable solutions.
Your code was so messy, that I cant figure out where the element <div class="slider-pager"></div> must sit? Its obviously inside the general container (on top of the image,) but at the top, bottom or maybe bottom center? I put it bottom center, as it looked the best there )
Removed $(".slider-large-image").css({'width' : imagereelwidth}); and added .slider-large-image, to make it rotate endless times.
I edited so much, that I forgot to update every single step. Anyways, it works. And also its endless now, meaning that there is no end for the carousel.
I wasted about 2 hours on this answer. So basically I was too lazy to compose it in the form of a plugin. Read this and follow those steps, if you want to put it in a plugin form.
If you want to add a function that automatically switches the slides, then use the jQuery doTimeout: Like setTimeout, but better!
I hope you are not taking my answer in a negative form. The community guidelines actually say, that we have to point the newbies to the correct path etc etch. I actually love you man :)
Live demo
http://jsfiddle.net/hobobne/PmXr2/
Full version code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Having trouble making this product slider. Please help? - Kalle H. Väravas answer</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style>
html, body {margin: 0px; padding: 0px;}
html, body, div, th, td, p, a {font-family: "Comic Sans MS", cursive; font-size: 12px; color: #000000;}
.cb {clear: both;}
#wrapper {width: 400px; margin: 0px auto;}
.main-slider {float: left; position: relative; margin-bottom: 10px; border: 0px solid #000; top: 25px; left: 0px; -moz-border-radius: 5px; border-radius: 5px; -moz-box-shadow: 0px 0px 30px 1px #999; -webkit-box-shadow: 0px 0px 30px 1px #999; box-shadow: 0px 0px 30px 1px #999; padding: 0px; color: #FFF; text-align: center; text-decoration: none; /*background-color: #CCC;*/}
.window {width: 300px; height: 200px; overflow: hidden; position: relative;}
.slider-large-image {position: relative; overflow: hidden; float: left; list-style-type: none; margin: 0px; padding: 0px;}
.slider-large-image li {margin: 0px; padding: 0px; float: left; display: inline-block;}
.slider-large-image li img {float: left; width: 300px; height: 200px;}
.slider-pager {position: relative; z-index: 2; margin: -40px auto 0px;}
.slider-pager a {margin: 0px 2px; padding: 2px; text-align: center; text-decoration: none; font-size: 20px; font-weight: bold; color: #ccc;}
.slider-pager a:hover,
.slider-pager a:active {background-color: #999; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px;}
.slider-pager a:hover {color: black;}
.slider-pager a.active {/* background-color and border-radius used to be here.. */}
</style>
</head>
<body>
<div id="wrapper">
<div class="main-slider">
<div class="window">
<ul class="slider-large-image">
<li><img src="http://images.sneakhype.com/wp-content/uploads/2010/12/Miley-Cyrus-300x200.jpg" /></li>
<li><img src="http://wa2.www.3news.co.nz/Portals/0-Articles/185340/miley-cyrus_reuters_420.jpg?width=300" /></li>
<li><img src="http://cdn.buzznet.com/media/jjr/headlines/2009/03/miley-cyrus-ryan-seacrest.jpg" /></li>
<li><img src="http://images.smh.com.au/2010/12/29/2112265/miley_cyrus_400-300x200.jpg" /></li>
</ul>
</div>
<div class="slider-pager">‹›</div>
</div>
<br class="cb" />
</div>
<script>
var imagewidth = $('.slider-large-image li').outerWidth();
var imagesum = $('.slider-large-image li img').size();
var imagereelwidth = imagewidth * imagesum;
$(".slider-large-image").css({'width' : imagereelwidth});
$('.slider-large-image li:first').before($('.slider-large-image li:last'));
$('.slider-large-image').css({'left' : '-' + imagewidth + 'px'});
rotatef = function (imagewidth) {
var left_indent = parseInt($('.slider-large-image').css('left')) - imagewidth;
$('.slider-large-image:not(:animated)').animate({'left' : left_indent}, 500, function() {
$('.slider-large-image li:last').after($('.slider-large-image li:first'));
$('.slider-large-image').css({'left' : '-' + imagewidth + 'px'});
});
};
rotateb = function (imagewidth) {
var left_indent = parseInt($('.slider-large-image').css('left')) + imagewidth;
$('.slider-large-image:not(:animated)').animate({'left' : left_indent}, 500, function(){
$('.slider-large-image li:first').before($('.slider-large-image li:last'));
$('.slider-large-image').css({'left' : '-' + imagewidth + 'px'});
});
};
$(".slider-pager a#b").click(function () {
rotateb(imagewidth);
return false;
});
$(".slider-pager a#f").click(function () {
rotatef(imagewidth);
return false;
});
</script>
</body>
</html>

Try this:
rotatef = function() {
$(".slider-large-image").animate({
"left" : "-="+imagewidth
},500 );
};
rotateb = function() {
$(".slider-large-image").animate({
"left": "+="+imagewidth
},500 );
};

Related

Drag and drop not working. JavaScript

I have a div named meniucolaj where I store some images and I want to drag and drop some of them into each canvas drawn in the div conturcanvas to make a photo collage.
To test the code, I only make the first photo to be dragged and the first canvas as drop location. I resized the images to be included in the gallery, but when i drop them into the canvas, i want them to be shown at bigger size into the canvas but i want to choose which part of the picture to be shown into the canvas. You know, as you got the opportunity to choose when making a collage, but the picture is not shown when dragging into the canvas. Can you help me?
Thank you in advance! I'm a beginner here.
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Photo Editor</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,400">
<link rel="stylesheet" type="text/css" href="testcollage2css.css">
</head>
<body> <!--continutul care va fi afisat pentru utilizatori-->
<!--atasare fisier video pentru background-->
<video autoplay muted loop id="bkgvideo">
<source src="C:\Users\AncaAlexandra\Desktop\Proiect multimedia\media\bkgcity.mp4" type="video/mp4">
</video>
<div class="header">
<nav class="navigation">
<div class="totalnavigheader">
<div class="navheader">
<img id="logo" src="C:\Users\AncaAlexandra\Desktop\Proiect multimedia\media\logocollage.png" alt="Logo cannot be displayed">
</div>
<div class="title">
<a id="name" href="#">Photo Collage</a>
</div>
</div>
</nav>
</div>
<div class="clasameniuri" id="meniuri">
<div class="meniucolaj" id="meniupoze">
<h4 id="titluimagini">Imagini</h4>
<ul class="listaimg" id="tabelimg">
<li class="elemimagini"><img class="imagini" id="dragelem" draggable="true" src="C:\Users\AncaAlexandra\Desktop\Proiect multimedia\media\dog1.jpg"></li>
<li class="elemimagini"><img class="imagini" src="C:\Users\AncaAlexandra\Desktop\Proiect multimedia\media\cat1.jpg"></li>
</ul>
</div>
</div>
<div class="containercolaj">
<div id="conturcanvas">
<canvas id="first"></canvas>
<canvas id="second"></canvas>
<canvas id="third"></canvas>
<script>
var canvas1 = document.getElementById("first");
var context1 = canvas1.getContext("2d");
<!--context.rect(1,0,296,100);
<!--context.stroke();-->
var canvas2 = document.getElementById("second");
var context2 = canvas1.getContext("2d");
var canvas3 = document.getElementById("third");
var context3 = canvas1.getContext("2d");
</script>
</div>
</div>
<script type="text/javascript" src="C:\Users\AncaAlexandra\Desktop\Proiect multimedia\testcollage2js.js"></script>
</body>
</html>
CSS:
/*stilizare background*/
body {
background: #1A2D4B;
position: relative;
color: black;
font-family: 'Open Sans', Helvetica, Arial, sans-serif;
font-size: 17px;
font-weight: 400;
overflow-x: hidden;
}
/*stilizare fisier video ca si background*/
#bkgvideo {
position: fixed;
right: 0;
bottom: 0;
left:0;
top:0;
min-width: 100%;
min-height: 100%;
}
.header {
position: relative;
left: 0;
right: 0;
top: 0;
z-index: 1002;
background-color: rgba(42, 59, 85, 0.6);
height: 80px;
box-shadow: 5px 10px 18px #888888;
border-radius: 10px;
}
.totalnavigheader > * {
display:inline-block;
}
#logo {
height: auto;
width: 60px;
vertical-align: middle;
margin-left:30px;
}
.title {
padding-left:10px;
padding-bottom:40px;
margin-left:10px;
}
#name {
text-decoration:none;
color:white;
}
.clasameniuri {
position:relative;
background-color: rgba(253, 242, 233, 0.8);
float:left;
width: 382px;
margin-top:8px;
margin-right:6px;
margin-left:5px;
height:560px;
box-shadow: 5px 10px 18px #888888;
border-radius: 10px;
}
#titluimagini {
color:rgb(56, 110, 147);
text-align:center;
}
.listaimg{
list-style-type:none;
}
.elemimagini{
margin:1px;
display:inline;
}
.imagini{
width:100px;
height:100px;
border:2px solid #000;
}
.containercolaj {
position:relative;
background-color:rgba(93, 109, 126,0.8);
float:right;
margin-top:8px;
margin-right:10px;
width:920px;
height:560px;
box-shadow:5px 10px 18px #888888;
border-radius: 10px;
}
#conturcanvas
{
width:490px;
height:490px;
border:2px solid #d3d3d3;
margin-top:20px;
margin-left:215px;
padding:20px;
background-color:gray;
}
#first{
border:1px solid #d3d3d3;
width:232px;
height:485px;
float:left;
}
#second{
border:1px solid #d3d3d3;
width:232px;
height:235px;
float:right;
margin-bottom:13px;
}
#third{
border:1px solid #d3d3d3;
width:232px;
height:235px;
float:right;
}
#first > .imagini{
width:100%;
height:100%;
}
JS:
var dragItem = document.getElementById("dragelem");
var dropCanvas = document.getElementById("first");
dragItem.ondragstart = function(evt) { //aceasta functie se apeleaza cand userul incearca sa mute elementul prin drag
//evt.dataTransfer.setData('key', 'dragelem');
evt.dataTransfer.setData('key', evt.target.id);
console.log("its dragging...");
}
dropCanvas.ondragover = function(evt) { //aceasta functie se apeleaza cand un element este adus catre o locatie valida de drop
evt.preventDefault();
console.log("its dragover..");
}
dropCanvas.ondrop = function(evt){ //aceasta functie se apeleaza cand se lasa prin drop in locatia valida elementul adus prin drag
var dropItem = evt.dataTransfer.getData('key');
evt.preventDefault();
console.log("its dropped..");
console.log(dropItem);
var myElement=document.getElementById(dropItem);
console.log(myElement);
var myNewElem = document.createElement('img'); // create a new image element
myNewElem.src = myElement.src;
dropCanvas.appendChild(myNewElem);
}
I suppose that dataTransfer not working..
That is not the correct way to draw an image on a canvas.
You need to get its CanvasRenderingContext2D:
context = dropCanvas.getContext("2d");
Then draw the image by calling
context.drawImage(myNewElem, x, y, more, arguments, here);
You can read about all the arguments drawImage can take here:
https://www.w3schools.com/tags/canvas_drawimage.asp

Context menu becomes unclickable above HTML video layer

I have created a custom context menu and would like it to be served as a playlist for my project.
However it becomes unclickable over my video frame.
Is this the nature of HTML video tag?
Here's my snippet
<!doctype html>
<html>
<head>
<title>Dee</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
html, body, .container{
height: 100%;
}
body{
font-family: verdana;
font-size: 10px;
}
.container{
background: #f6f6f6;
}
.context-menu {
width: 200px;
height: auto;
box-shadow: 0 0 20px 0 #ccc;
position: absolute;
display: none;
}
.context-menu ul{
list-style: none;
padding: 5px 0 5px 0;
}
.context-menu ul li:not(.separator){
padding: 10px 5px 10px 5px;
border-left: 4px solid transparent;
cursor: pointer;
}
.context-menu ul li:hover{
/*background: #eee;*/
background: #fff;
border-left: 4px solid #666;
}
.separator{
height: 1px;
background: #dedede;
/*background: #fff;*/
margin: 2px 0 2px 0;
}
.videoClass{
background: #fff;
border-color: #fff;
}
</style>
</head>
<body>
<div class="container" oncontextmenu="return showContextMenu(event);">
<div id="contextMenu" class="context-menu">
<ul>
<li>List</li>
<li>List</li>
<li>List</li>
<li class="separator"></li>
<li>List</li>
</ul>
</div>
<video id="myVideo" class="videoClass" controls width="500" src="trailer.mp4"></video>
</div>
<script type="text/javascript">
window.onclick = hideContextMenu;
window.onkeydown = listenKeys;
var contextMenu = document.getElementById('contextMenu');
function showContextMenu(){
contextMenu.style.display = 'block';
contextMenu.style.left = event.clientX + 'px';
contextMenu.style.top = event.clientY + 'px';
return false;
}
function hideContextMenu(){
contextMenu.style.display = 'none';
}
function listenKeys(event){
var keyCode = event.which || event.keyCode;
if (keyCode == 27) { //27 means escape key
hideContextMenu();
}
}
</script>
</body>
</html>
New Answer
I found that an even simpler solution to your problem exists. If you just put the video tag before the context menu, that does the trick:
<video id="myVideo" class="videoClass" controls width="500" src="trailer.mp4"></video>
<div id="contextMenu" class="context-menu">
<ul>
<li>List</li>
<li>List</li>
<li>List</li>
<li class="separator"></li>
<li>List</li>
</ul>
</div>
Original Answer
In my limited experience I haven't loved using z-indexes, so I'll look for another solution, but I put styles of z-index: 1 on video class and z-index: 2 on the context menu class, and that did that trick (in chrome at least, I didn't try any other browsers).

Resize image upon click

Alright so, I need to resize an image on click (according to what the user clicks) a selection of 50%, 100%, and 150%. I also need to include a message that tells the user what % the picture is at once it has been scaled.
I think what I have is correct or in the right direction (well obviously not, since it's not working) wondering if anyone can help me out here... this is what I have currently:
<!doctype html>
<html lang="en">
<head>
<title>Change Image Size</title>
<style>
body {width: 80%; margin: auto;}
header {border-bottom: 1px solid #aaa;}
header h1 {font-size: 1.5em;}
nav {float: left; width: 15%; margin: 2% 2%; }
main {float: left; width: 60%; border: 0px black solid; margin: 2% 2%; height: 500px; padding: 1em;}
main img {border: 1px black solid;}
nav span {display: inline-block; width:100px; height: 30px; border: 1px solid black; border-radius: 10px; text-align: center; margin-bottom: 1em; background-color: green; color: white;}
</style>
<script>
function init (){
var object = document.getElementById('div1');
document.getElementById('small').onclick = function () {change('div1', '50%')};
document.getElementById('medium').onclick = function () {change('div1', '100%')};
document.getElementById('large').onclick = function () {change('div1', '150%')};
}
function change (id, width){
document.getElementById(id).style.width = width;
document.getElementById(id).style.height = 'auto';
}
window.onload = init;
</script>
</head>
<body>
<header><h1>CTEC 4350 DHTML Exercise: Change Image Size</h1></header>
<nav>
<span id="small">50%</span>
<span id="medium">100%</span>
<span id="large">150%</span>
</nav>
<main>
<p> This image is displayed at 150% of its orignal size. (The number of % changes based on the size option picked by the user.)</p>
<div id="div1">
<img src="http://omega.uta.edu/~cyjang/ctec4350/labex/dhtml/alice28a.gif">
</div>
</main>
</body>
</html>
welp i'm still having issues with this. I need to find out the current size, and then add on to it.... but i'm not sure if i'm doing it correctly... i've changed my code a little, but not even sure if i'm going the right direction. Nothing is changing, and i'm not sure why o.o
<!doctype html>
<html lang="en">
<head>
<title>Change Image Size</title>
<style>
body {width: 80%; margin: auto;}
header {border-bottom: 1px solid #aaa;}
header h1 {font-size: 1.5em;}
nav {float: left; width: 15%; margin: 2% 2%; }
main {float: left; width: 60%; border: 0px black solid; margin: 2% 2%; height: 500px; padding: 1em;}
main img {border: 1px black solid;}
nav span {display: inline-block; width:100px; height: 30px; border: 1px solid black; border-radius: 10px; text-align: center; margin-bottom: 1em; background-color: green; color: white;}
</style>
<script>
function init (){
document.getElementById('small').onclick = function () {change('div1',50,50)};
document.getElementById('medium').onclick = function () {change('div1',100,100)};
document.getElementById('large').onclick = function () {change('div1',150,150)};
}
function change(id, y, x){
var img = document.getElementById('div1');
img.style.width = img.offsetWidth + y + "px";
img.style.height = img.offsetHeight + x + "px";
document.getElementById(id).style.width = y;
document.getElementById(id).style.height = x;
}
window.onload = init;
</script>
</head>
<body>
<header><h1>CTEC 4350 DHTML Exercise: Change Image Size</h1></header>
<nav>
<span id="small">50%</span>
<span id="medium">100%</span>
<span id="large">150%</span>
</nav>
<main>
<p> This image is displayed at 150% of its orignal size. (The number of % changes based on the size option picked by the user.)</p>
<div id="div1">
<img src="http://omega.uta.edu/~cyjang/ctec4350/labex/dhtml/alice28a.gif" height="400px" width="400px">
</div>
</main>
</body>
</html>

Using JavaScript to return the value of the click li item and populating the textarea with the result

I could not find, for the life of me a jQuery less way to accomplish getting the value of the clicked li item and populating the textarea box id="result" with the clicked result.
How can this be done? This seems like rocket science to me.
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#container {
width: 200px;
float: left;
font-family: Arial;
font-size: 10pt;
position:relative;
}
#one {
height: 200px;
border: 1px solid red;
display: none;
position:absolute;
background: #C0C0C0;
}
#two {
width: 8px;
height: 8px;
border: 1px solid blue;
float: left;
position:absolute;
}
#menu, ul {
list-style: none;
margin: 0;
cursor: default;
width:194px;
padding:6px;
}
#menu, ul, li {
padding: 2px;
}
#menu li:hover{
background: blue;
color: #fff;
}
#result {
border: 1px solid #000;
width: 206px;
}
</style>
<script type="text/javascript">
function showMenu(){
document.getElementById("one").style.display="block";
}
function hideMenu(){
document.getElementById("one").style.display="none";
}
</script>
</head>
<body>
<div id="container">
<div id="one" onclick="hideMenu()">
<ul id="menu">
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
</div>
<div id="two"><img src="images/arrow_double.png" onclick="showMenu()"></div>
<br>
<textarea id="result"></textarea>
</body>
</html>
This is my suggestion, though it's not tested in Internet Explorer:
// pick a name that's useful to you:
function textToTextArea (e) {
/* most browsers pass the event object to the function,
IE does, or did, not; here we use the passed-event if it's
available, or the window.event if it's not there (implying IE):
*/
e = e || window.event;
// finding out the text property we can access to retrieve an element's text:
var text = 'textContent' in document ? 'textContent' : 'innerText';
/* getting the textarea by its 'id',
and setting its innerHTML to be equal to the text of the clicked 'li':
*/
document.getElementById('result').innerHTML = e.target[text];
}
var list = document.getElementById('menu');
list.onclick = textToTextArea;
JS Fiddle demo.
Incidentally, in jQuery the above could be abbreviated to:
$('li').click(function(){
$('#result').val($(this).text());
});
JS Fiddle demo.
It's not always the best solution, but it saves a lot of time and handles cross-browser issues very well (saving us from normalizing for the event object); and while you don't (and shouldn't) have to justify not-using jQuery, sometimes it's worth remembering that there are other, more useful, things we can all be doing rather than simply avoiding it for arbitrary (and in this case unspecified) reasons.
DEMO jsFiddle
Description
This is a pure JavaScript answer, it uses the this on each li item. This event binding can be done in the window.onload event with a for loop if you'd prefer. It works on all browsers, the layout looks wrong to me but as that isn't the question I didn't care.
Let me know if you need more assistance.
HTML
<div id="container">
<div id="one" onclick="hideMenu()">
<ul id="menu">
<li onclick="itemPicked(this)">Item 1</li>
<li onclick="itemPicked(this)">Item 2</li>
</ul>
</div>
</div>
<div id="two">
<img src="http://realestatecommunities.com/wp-content/uploads/2011/01/blue-arrow-down.jpg" height="20px" width="20px" onclick="showMenu()" />
</div>
<br/>
<textarea id="result"></textarea>
JS
function showMenu() {
document.getElementById("one").style.display = "block";
}
function hideMenu() {
document.getElementById("one").style.display = "none";
}
function itemPicked(el) {
document.getElementById("result").value = el.textContent;
}
CSS
#container {
width: 200px;
float: left;
font-family: Arial;
font-size: 10pt;
position:relative;
}
#one {
height: 200px;
border: 1px solid red;
display: none;
position:absolute;
background: #C0C0C0;
}
#two {
width: 8px;
height: 8px;
border: 1px solid blue;
float: left;
position:absolute;
}
#menu, ul {
list-style: none;
margin: 0;
cursor: default;
width:194px;
padding:6px;
}
#menu, ul, li {
padding: 2px;
}
#menu li:hover {
background: blue;
color: #fff;
}
#result {
border: 1px solid #000;
width: 206px;
}

jQuery 'Slides' not functioning

I am using the script at slidesjs.com (http://slidesjs.com/examples/simple/), trying to put it into my webpage. I decided to use a script and not just attempt to write it in jQuery myself as I could not get it to work, and I suspect the same problem is occurring here..whatever it may be. Here is the page source code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>John Smith</title>
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="js/slides.min.jquery.js"></script>
<script>
$(function(){
$('#sections').slides({
preload: true,
generateNextPrev: true
container: 'sections_container'
});
});
Web Developer
John Smith
This text is purely to fill space until I add real content.
</p>
<p id="contact">If you'd like to contact me about anything, don't hesitate to email me at j.smith#example.com</p>
<div id="social_media"><img src="images/social_media/facebook.png" /><img src="images/social_media/twitter.png" /><img src="images/social_media/flickr.png" /><img src="images/social_media/youtube.png" /></div>
<div class="clear"></div>
</div>
<div>
<h3 class='heading'>Information</h3>
<p>
<ul>
<li><strong>Name:</strong> John Smith</li>
<li><strong>Location:</strong> Sydney, Australia</li>
</ul>
</p>
<br />
<h3 class='heading'>Achievements & Experience</h3>
<p>
<ul>
<li>Worked at Google</li>
<li>Worked at McDonalds</li>
</ul>
</p>
</div>
</div>
</div>
</body>
The CSS if anyone is interested.
* {
padding:0;
margin:0;
}
a {
color:white;
text-decoration:none;
}
a:hover {
text-decoration:underline;
}
body {
background: url('images/background.png');
font-size:12px;
font-family:arial;
color:#E5E5E5;
}
.sections_container {
width:768px;
display:none;
}
li {
margin-left:20px;
}
.sections_container div {
margin: 0 auto 4px auto;
width:768px;
background: rgba(0,0,0,0.3);
padding:10px;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border:1px #7F7F7F solid;
box-shadow: inset 2px 2px #191919;
display:block;
}
.pagination {
list-style:none;
margin:0;
padding:0;
}
/*
Optional:
Show the current slide in the pagination
*/
.pagination .current a {
color:red;
}
.top {
margin: 15px auto 4px auto;
width:768px;
background: rgba(0,0,0,0.3);
padding:10px;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border:1px #7F7F7F solid;
box-shadow: inset 2px 2px #191919;
}
.section {
margin: 0 auto 4px auto;
width:768px;
background: rgba(0,0,0,0.3);
padding:10px;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border:1px #7F7F7F solid;
box-shadow: inset 2px 2px #191919;
}
h1,h2,h3,h4,h5,h6 {
}
#subtitle {
color:#919191;
text-align:right;
}
#title {
text-align:right;
}
#profile {
float:left;
height:52px;
width:52px;
}
#youtube_video {
float:right;
padding-left:10px;
}
#contact {
margin-top:8px;
font-weight:bold;
font-style:italic;
}
#social_media {
margin-top:40px;
text-align:center;
}
.heading {
text-align:center;
}
.demphasis {
color:#9E9E9E;
}
.clear {
clear:both;
}
Thanks!
OK. I can see a couple of wee issues.
Firstly, in your CSS you reference .section_container, whereas in the HTML it's <div class="sections_container"> - note the 's' in the middle. Same for section/s.
Secondly, it looks like SlideJS defaults to "slides_container". Either rename your divs, or set the container name when you're initializing it:
$('#sections').slides({
preload: true,
generateNextPrev: true,
container: 'sections_container'
});
I've created a little jsFiddle at http://jsfiddle.net/jCFeQ/ - it still has some issues, but is closer to working than when we started. Change those two things and see how you go.
It appears you have a typo in your CSS that refers to a missing HTML element:
.section_container div {
...
}
Instead of .section_container, your CSS should be .sections_container (note the plural) which matches your HTML:
<div class="sections_container">
<div></div>
...
<div></div>
</div>
Additionally, the Slides jQuery Plugin expects an HTML element with a class of .slides_container as the parent of the slides.
Since you've deviated from the default markup, you'll need to supply the plugin's container option in your jQuery function:
<script>
$(function(){
$('.sections_container').slides({
preload: true,
generateNextPrev: true,
container: 'sections_container'
});
});
</script>
Silly question, but given you've copied the code letter-for-letter from that example: Is the file you reference in <script src="js/slides.min.jquery.js"></script> actually there - do you have a js folder with that file inside it?
Also version 1.5.1 of jQuery is quite out-of-date. It shouldn't make any difference, but you might as well change that to 1.7.1

Categories

Resources