starting smoothdivscroll at random position - jquery-plugins

I am using smoothdivscroll plugins. I want to start scrolling at different position f text when page is reloaded or refreshed. How can I do this?
Thanks.

You would have to write some code yourself for this special function. SmoothDivScroll has an option, startAtElementId, that you can use to tell the scroller to start at the position of a certain element (id). If you are loading the content of the scroller dynamically on the server side (using PHP, ASP.NET or something similar) you could make the server side code pick one of the content elements and add a certain id to it. Then you would set the option like this:
<script type="text/javascript">
$(document).ready(function() {
$("#makeMeScrollable").smoothDivScroll({
startAtElementId: "yourID"
});
});
</script>
You could also use the method jumpToElement. First you would initialize the plugin. Then directly after you would make a call to jumpToElement and tell it to jump to an element with a random number between 1 and the total number of elements you have loaded. This is not tested in any way, but I think it would look something like this:
<script type="text/javascript">
$(document).ready(function() {
// Initialize the plugin
$("#makeMeScrollable").smoothDivScroll();
// Randomize an element
var numberOfElements = $("#scrollableArea *").length;
var randomNumber = Math.floor(Math.random() * numberOfElements + 1 );
$("#makeMeScrollable").smoothDivScroll("jumpToElement", "number", randomNumber);
});
</script>
As I said, the above code has not been tested, but something along those lines should work.
Good luck!

Related

fullpage.js - Get the active Section Index number

The is pretty straightforward. I'm trying to find a way to get the active section Index number. I want the number to change when you scroll to a different section.
What I want to achieve:
Demo
Make use of fullPage.js callbacks or state classes.
Notice you have the slideIndex on the callbacks:
afterSlideLoad: function(anchorLink, index, slideAnchor, slideIndex){
//do whatever here
$(body).append(slideIndex);
},
Because the afterSlideLoad callback won't get fired on section change, you'll need to also make use of the afterLoad callback and get the slide number by using the state classes added by fullPage.js:
afterLoad: function(anchorLink, index){
var slideNumber = $('.fp-section.active').find('.fp-slide.active').index() + 1
//do whatever here
$(body).append(slideNumber);
}

Extra row atop Kendo Treelist

We have a Kendo TreeList that works fine. Data shows, everything shows in the hierarchy correctly. The problem is, we need to group each two columns into another "superset" group.
The column headings (the names above are not real) are too long if not grouped as shown, and they lose useful context.
I tried adding an HTML table above the TreeList, but that doesn't look right. And it doesn't work if the user resizes the columns. Also the toolbar (for Excel export) is in the way, so it doesn't even look like it's part of the TreeList.
I also looked at wrapping the text in the columns, but from what I've seen, that's really iffy too.
It seems like an extra row as shown above (with the ability to merge some columns, like with an HTML table) is the best way to go. Despite scouring the web, I couldn't find a way to do this. Is this even possible with a Kendo TreeList?
This has been solved. Not by me, but by another developer on our team who's insanely good at JavaScript.
The trick is to edit the TreeList's HTML and CSS through JavaScript. You can bind to any event, but we do it on page load:
<script>
$(document).ready(function () {
// anything in here will get executed when the page is loaded
addTopRowToTreeList();
});
function addTopRowToTreeList() {
// grab the thead section
// your HTML may be different
var foo = $('#MyTreeList').children('div.k-grid-header').children('div.k-grid-header-wrap');
var tableChild = foo.children('table');
var headChild = tableChild.children('thead');
var bottomRow = headChild.children('tr');
var topRow = $('<tr>').attr('role', 'row');
// bottom cell should draw a border on the left
bottomRow.children('th').eq(0).addClass('k-first');
// add blank cell
var myNewCell = $('<th>').addClass('k-header').attr('colspan', '1')
var headerString = '';
var headerText = $('<span>').addClass('k-link').text(headerString);
myNewCell.append(headerText);
topRow.append(myNewCell);
// ... add remaining cells, like above
headChild.prepend(topRow);
}
</script>
That's all there is to it!

How to load images in order with unveil.js

I am using unveil.js to load a site more quickly.
I have a white div that blocks the content, which I want to disappear after the first images have loaded.
I though I could just count the images, but I realize that some of the latter ones could just load first (which normally happens because there is a threshold parameter which loads the ones that follow on the scroll).
Could someone help me with a smart way to do this?
Here's my code and the crappy solution:
$("img").unveil(2000, function() {
$(this).load(function(){
if(imageCount >= 4){
$(".white-cover").fadeOut("slow");
imageCount = 0;
}
Since unveil changes the src attribute, you can use jQuery to listen to an attribute change which indicate that an image has been replaced.
$(document).ready(function(){
$("#selected-date-range").change(function(){
alert( $("#selected-date-range").attr("value") );
});
});
In case I understand your problem correctly, I would use:
$(document).ready(function(){
// all images are loaded
}
Does this help you?

jquery load order affects list

I have the following jQuery to load status to a profile page using a ul with li status items.
The each() takes items from a JSON callback and the load() is supposed to ensure that the image is available before the li is created:
(showpic() gives me a well-formed url to use.)
function showStatus(data){
var jsondata = $.parseJSON(data);
var testText = "";
$('#doNews').empty();
$('#doNews').append($('<ul/>', {"class": "newsList", id: "theNews"}));
$.each(jsondata, function(i, item){
$('<img src="' + showpic(item[3]) + '" class="newsImage">')
.load(function(){
$(this)
.appendTo($('#theNews'))
.wrap($('<li>', {"class": "newsItem"}))
.closest('li')
.append(item[5])
});
});
$("#statustext").val('');
}
the problem is that the status feed now seems to be written to the page in the order the images load. i.e., instead of being written according to the JSON item order, the li s are written in the order of loaded images (this has the effect of grouping status by user, not writing it out by date, as in the JSON).
So...
how would I both write items in the JSON order and still wait for the img to load?
By the way, I looked at this qn:
jQuery each() and load() ordering
and it seems to be on the right track, but when I tried using hide() and then show()inside the load() function, it never seemed to be called, and the img remained hidden. Please give me a simple example if this is the solution you suggest.
Thanks!
Either:
1) Maintain a counter & timeout, count the # of images to load at start, create the items as "hidden", countdown the number of unloaded as they load, then (when counter hits 0 or timer times out) display everything;
or:
2) Create the items as hidden, and show them in the 'load' event. Items will appear in arbitrary order, but will end up being correctly ordered.
Here's a possible example: your code isn't very clear as to what structure is being built & where appended, so this is just a rough (but clearly coded) outline.
Try using intermediate variables more, rather than vast fabulous jQuery constructions, in your own. It will help you debug it.
console.log('loading news items');
$.each( jsondata, function(i, item){
console.log(' item', showpic(item[3]), item[5]);
var img = $('<img src="' + showpic(item[3]) + '" class="newsImage" >');
var element = img.wrap($('<li>', {"class": "newsItem", "style": "display:none;"}))
.closest('li')
.append(item[5]);
element.appendTo( $('#theNews'));
// when the IMG loads, find it's surrounding LI.. and show it.
img.load( function(){
console.log(' loaded', $(this).attr('src'));
$(this).closest('li').show();
});
// put a timer & show it anyway after 8s, if img still hasn't loaded.
element.delay( 8000).show(0);
});
You will also notice logging in the code. Logging is good software practice. Console.log is not available on IE, so you should eventually shim it or use a small library function of your own.
Fundamentally, you have to get away from adding to the DOM inside the 'load' event. That's what's causing the mis-ordering. Add to the DOM in the jsondata each() function, or in a separate well-structured bit of code that guarantees correct ordering.

iPhone/iPad touch event to trigger different div

Seems like an easy solution but I can't wrap my head around it. I have four divs like the one below
<div ontouchstart="touchStart(event)" ontouchmove="touchMove (event)" ontouchend="touchEnd (event)" class="c1" id="c1">copy1</div>
The code for touchStart is very simple at the moment:
function touchStart (e) {
e.preventDefault();
e.target.style.color="#ff0000";
return false;
}
The issue is how can the touch functions target a different div (not the target that was touched) and change it's opacity from 0 to 1 in touchStart, then from 1 to 0 in touchEnd.
Thanks
I'm no expert but here's my two cents and hopefully will help you find a solution
With jquery, clicking one element can trigger a different one and change the target's css opacity attribute like:
$("#div_thats_touched").click(function(){
$("#target_div").css("opacity","1");
})
in javascript it would be something like
this.onClick(function(){
target=document.getElementById('target');
target.style.opacity("1");
});
or if you want to trigger the event of another element:
$("#div_thats_touched").click(function(){
$("#target_div").trigger("click");
})
And instead of having touchstart and touchend, you could use jquery's toggle method to combine them into one method (maybe)
http://api.jquery.com/toggle/
http://api.jquery.com/trigger/

Resources