Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*.iml
.sass-cache
.bowerrc
.idea
bower_components
node_modules
*.sublime*
*.zip
.DS_Store
*/.DS_Store
.DS_Store?
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
.grunt
53 changes: 53 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
options: {
stripBanners: true,
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %> */' +
'\n',
},
pub: {
src: './dist/jquery.smint.js',
dest: './dist/jquery.smint.min.js'
}
},
jshint: {
files: ['*.js'],
beforeconcat: ['.js'],
afterconcat: ['<%= pkg.name %>.js'],
options: {
ignores: ['jquery.*']
}
},
uglify: {
options: {
stripBanners: true,
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %> */' +
'\n',
},
pub: {
files: {
'./dist/jquery.smint.min.js': ['./dist/jquery.smint.min.js']
}
}
}
});

// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-concat');

// test task(s)
grunt.registerTask('test', ['jshint', 'concat']);
// Default task(s).
grunt.registerTask('default', ['concat', 'uglify']);

};
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
SMINT
=====
# SMINT

SMINT is a simple plugin for lovers of one page websites

What exactly does it do again?
### What exactly does it do again?
Smint is a simple jQuery plugin that helps with the navigation on one page style websites.

It has 2 main elements, a sticky navigation bar that stays at the top of the page while you scroll down and menu buttons that automatically scroll the page to the section you clicked on.


So how do I use it?
### So how do I use it?
Create a 'div' for your menu and give it a class name.

Inside this you put your 'a' tags and give each one an #id. This is your menu wrapper and it should be set to position:absolute
Expand All @@ -22,19 +20,24 @@ Add the jquery.smint.js to the head section of your page.

Call the function with the following script:

``` js
$(document).ready( function() {
$('.subMenu').smint();
$('.subMenu').smint();
});
```

You can replace .subMenu with the class name of your menu

To give you a littl emore flexability, the class.fxd gets added to your menu when it becomes fixed to the top of the screen, allowing some extra styleing to be added if needed.

Options
### Options
SMINT is a simple plugin, so only has an option for how fast the page scrolls.

``` js
$('.subMenu').smint({
'scrollSpeed' : 1000
'scrollSpeed' : 1000,
'mySelector': 'section'
});
```

The default speed is 500 (half a second) but you can now set that to be whatever you like.
218 changes: 218 additions & 0 deletions dist/jquery.smint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/**
* SMINT JQuery Plugin v3.0.1
* Copyright 2012-2016 Robert McCracken
* SMINT V1.0 by Robert McCracken
* SMINT V2.0 by robert McCracken with some awesome help from Ryan Clarke (@clarkieryan) and mcpacosy ‏(@mcpacosy)
* SMINT V3.0 by robert McCracken with some awesome help from Ryan Clarke (@clarkieryan) and mcpacosy ‏(@mcpacosy)
*
* SMINT is my first dabble into jQuery plugins!
* http://www.outyear.co.uk/smint/
* If you like Smint, or have suggestions on how it could be improved, send me a tweet @rabmyself
**/

/**
* SMINT JQuery
* @version 3.0.1
* @author Robert McCracken
* @license The MIT License (MIT)
*/

(function(){
$.fn.smint = function( options ) {

var settings = $.extend({
'scrollSpeed' : 500,
'mySelector' : 'div'
}, options);

// adding a class to users div
$(this).addClass('smint');

//Set the variables needed
var optionLocs = new Array(),
lastScrollTop = 0,
menuHeight = $(".smint").height(),
smint = $('.smint'),
smintA = $('.smint a'),
myOffset = smint.height();

if ( settings.scrollSpeed ) {
var scrollSpeed = settings.scrollSpeed
}

if ( settings.mySelector ) {
var mySelector = settings.mySelector
};

var scrollTo = function( hash ) {

if (smint.hasClass('fxd')) {
var goTo = $(mySelector+'.'+ hash).position().top-myOffset;
} else {
var goTo = $(mySelector+'.'+ hash).position().top-myOffset;
}
console.log("goto:" + goTo + " myOffset" + myOffset);
$("html, body").stop().animate({ scrollTop: goTo }, scrollSpeed);

if ($(this).hasClass("extLink"))
{
return false;
}

};

var $chainableReturn = smintA.each( function(index) {

var id = $(this).attr('href').split('#')[1];

if (!$(this).hasClass("extLink")) {
$(this).attr('id', id);
}

//Fill the menu
optionLocs.push(Array(
$(mySelector+"."+id).position().top-menuHeight,
$(mySelector+"."+id).height()+$(mySelector+"."+id).position().top, id)
);

///////////////////////////////////

// get initial top offset for the menu
var stickyTop = smint.offset().top;

// check position and make sticky if needed
var stickyMenu = function(direction){

// current distance top
var scrollTop = $(window).scrollTop()+myOffset;

// if we scroll more than the navigation, change its position to fixed and add class 'fxd', otherwise change it back to absolute and remove the class
if (scrollTop > stickyTop+myOffset) {
smint.css({ 'position': 'fixed', 'top':0,'left':0 }).addClass('fxd');

// add padding to the body to make up for the loss in heigt when the menu goes to a fixed position.
// When an item is fixed, its removed from the flow so its height doesnt impact the other items on the page
$('body').css('padding-top', menuHeight );
} else {
smint.css( 'position', 'relative').removeClass('fxd');
//remove the padding we added.
$('body').css('padding-top', '0' );
}

// Check if the position is inside then change the menu
// Courtesy of Ryan Clarke (@clarkieryan)
if(optionLocs[index][0] <= scrollTop && scrollTop <= optionLocs[index][1]){
if(direction == "up"){
$("#"+id).addClass("active");
$("#"+optionLocs[index+1][2]).removeClass("active");
} else if(index > 0) {
$("#"+id).addClass("active");
$("#"+optionLocs[index-1][2]).removeClass("active");
} else if(direction == undefined){
$("#"+id).addClass("active");
}
$.each(optionLocs, function(i){
if(id != optionLocs[i][2]){

$("#"+optionLocs[i][2]).removeClass("active");
}
});
}
};

// run functions
stickyMenu();

// run function every time you scroll
$(window).scroll(function() {
//Get the direction of scroll
var st = $(this).scrollTop()+myOffset;
if (st > lastScrollTop) {
direction = "down";
} else if (st < lastScrollTop ){
direction = "up";
}
lastScrollTop = st;
stickyMenu(direction);

// Check if at bottom of page, if so, add class to last <a> as sometimes the last div
// isnt long enough to scroll to the top of the page and trigger the active state.

if($(window).scrollTop() + $(window).height() == $(document).height()) {
smintA.removeClass('active')
$(".smint a:not('.extLink'):last").addClass('active')

} else {
smintA.last().removeClass('active')
}
});

///////////////////////////////////////

$(this).on('click', function(e){
// gets the height of the users div. This is used for off-setting the scroll so the menu doesnt overlap any content in the div they jst scrolled to
var myOffset = smint.height();

// stops hrefs making the page jump when clicked
e.preventDefault();

// get the hash of the button you just clicked
var hash = $(this).attr('href').split('#')[1];

return scrollTo(hash);

var goTo = $(mySelector+'.'+ hash).offset().top-myOffset;
console.log("goto:" + goTo + " myOffset" + myOffset);
// Scroll the page to the desired position!
$("html, body").stop().animate({ scrollTop: goTo }, scrollSpeed);

// if the link has the '.extLink' class it will be ignored
// Courtesy of mcpacosy ‏(@mcpacosy)
if ($(this).hasClass("extLink"))
{
return false;
}

});


//This lets yo use links in body text to scroll. Just add the class 'intLink' to your button and it will scroll

$('.intLink').on('click', function(e){
var myOffset = smint.height();

e.preventDefault();

var hash = $(this).attr('href').split('#')[1];

return scrollTo(hash);

if (smint.hasClass('fxd')) {
var goTo = $(mySelector+'.'+ hash).position().top-myOffset;
} else {
var goTo = $(mySelector+'.'+ hash).position().top-myOffset;
}
console.log("goto:" + goTo + " myOffset" + myOffset);
$("html, body").stop().animate({ scrollTop: goTo }, scrollSpeed);

if ($(this).hasClass("extLink"))
{
return false;
}

});

});

// see if we have a hash and if so, jump to it using smint
var hashLocation = location.hash;
if(hashLocation && hashLocation != '#') {
var hashClass = hashLocation.replace(/^(#)/, '');
scrollTo(hashClass);
}

return $chainableReturn;
};

$.fn.smint.defaults = { 'scrollSpeed': 500, 'mySelector': 'div'};
})(jQuery);
2 changes: 2 additions & 0 deletions dist/jquery.smint.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "jquery.smint",
"version": "3.0.1",
"devDependencies": {
"grunt": "*",
"grunt-contrib-jshint": "*",
"grunt-contrib-nodeunit": "*",
"grunt-contrib-uglify": "*",
"grunt-contrib-concat": "*",
"grunt-contrib-cssmin": "*"
},
"description": "ERROR: No README data found!",
"main": "Gruntfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "BSD"
}