Skip to content
Browse files

implemented smooth scroll

  • Loading branch information...
1 parent 5254c26 commit 21e43dfcf7eb4d57566915fef10bf31e55f338c0 @k2nr committed Oct 10, 2011
Showing with 103 additions and 37 deletions.
  1. +8 −0 options.html
  2. +6 −0 options.js
  3. +1 −34 search.js
  4. +1 −0 settings.js
  5. +87 −3 view.js
View
8 options.html
@@ -71,6 +71,14 @@ <h1 id="navbar-content-title">Vichrome</h1>
</span>
</label>
</div>
+ <div>
+ <label>
+ <input id="smoothScroll" type="checkbox" />
+ <span class="option">
+ Smooth Scroll
+ </span>
+ </label>
+ </div>
</div>
</section>
<section>
View
6 options.js
@@ -78,6 +78,12 @@ function onSettings(msg) {
setSetting("disableAutoFocus", $(this).is(':checked'));
});
+ $('#smoothScroll')
+ .attr( 'checked', settings.smoothScroll )
+ .change( function() {
+ setSetting("smoothScroll", $(this).is(':checked'));
+ });
+
$('[name="newTabPage"][value="'+settings.defaultNewTab+'"]')
.attr( 'checked', true );
$('[name="newTabPage"]')
View
35 search.js
@@ -26,39 +26,6 @@ vichrome.search = {};
});
}
- function adjustScreenToSearchResult(pos) {
- // TODO:try to dicide more meaningful value for the padding
- if( !isWithinScreen( pos.left, pos.top, 10 ) ) {
- var newX = pos.left - window.innerWidth / 2,
- newY = pos.top - window.innerHeight / 2;
-
- if( newX > document.body.scrollLeft - window.innerWidth ) {
- newX = document.body.scrollLeft - window.innerWidth;
- }
- if( newY > document.body.scrollHeight - window.innerHeight ) {
- newX = document.body.scrollHeight - window.innerHeight;
- }
-
- window.scrollTo( newX, newY );
- }
- }
-
- function isWithinScreen(x, y, padding) {
- // padding is for visiblity, which if pos is on the edge of the screen
- // it's a little bit difficult to see the result word.
- if( x + padding > window.pageXOffset + window.innerWidth ||
- x - padding < window.pageXOffset ) {
- return false;
- }
-
- if( y + padding > window.pageYOffset + window.innerHeight ||
- y - padding < window.pageYOffset ) {
- return false;
- }
-
- return true;
- }
-
function getResultCnt() {
return _sortedResults.length;
}
@@ -167,7 +134,7 @@ vichrome.search = {};
if( span ) {
$('span').removeClass('highlightFocus');
span.addClass('highlightFocus');
- adjustScreenToSearchResult( span.offset() );
+ span.scrollTo();
vichrome.view.setStatusLineText( (pos+1) + " / " + getResultCnt() );
}
} else {
View
1 settings.js
@@ -13,6 +13,7 @@ var SettingManager = {
"commandWaitTimeOut" : 2000,
"fModeAvailableKeys" : "fdsaghjklwertyuiovbcnm",
"disableAutoFocus" : false,
+ "smoothScroll" : false,
"wrapSearch" : true,
"incSearch" : true,
"ignoreCase" : true,
View
90 view.js
@@ -1,5 +1,87 @@
vichrome.widgets = {};
+$.fn.extend({
+ isWithinScreen : function() {
+ var offset = $(this).offset(),
+ padding = 10;
+ // padding is for visiblity, which if pos is on the edge of the screen
+ // it's a little bit difficult to see the result word.
+ if( offset.left + padding > window.pageXOffset + window.innerWidth ||
+ offset.left - padding < window.pageXOffset ) {
+ return false;
+ }
+
+ if( offset.top + padding > window.pageYOffset + window.innerHeight ||
+ offset.top - padding < window.pageYOffset ) {
+ return false;
+ }
+
+ return true;
+ },
+
+ scrollTo : function(x, y, speed) {
+ var top, left,
+ offset = $($(this).get(0)).offset(),
+ newX = offset.left - window.innerWidth / 2,
+ newY = offset.top - window.innerHeight / 2;
+
+ if( newX > document.body.scrollLeft - window.innerWidth ) {
+ newX = document.body.scrollLeft - window.innerWidth;
+ }
+ if( newY > document.body.scrollHeight - window.innerHeight ) {
+ newX = document.body.scrollHeight - window.innerHeight;
+ }
+
+ if( x === undefined && y === undefined &&
+ $(this).isWithinScreen() ) {
+ return $(this);
+ }
+
+ if( x === undefined ) {
+ left = newX;
+ } else {
+ left = x;
+ }
+
+ if( y === undefined ) {
+ top = newY;
+ } else {
+ top = y;
+ }
+
+ if( speed === undefined ) {
+ speed = 80;
+ }
+
+ if( !vichrome.model.getSetting("smoothScroll") ) {
+ speed = 0;
+ }
+
+ $(document.body).animate({scrollTop : top, scrollLeft : left}, speed);
+ return $(this);
+ },
+
+ scrollBy : function(x, y, speed) {
+ var top, left,
+ win_left = window.pageXOffset,
+ win_top = window.pageYOffset;
+
+ top = win_top + y;
+ left = win_left + x;
+
+ if( speed === undefined ) {
+ speed = 80;
+ }
+
+ if( !vichrome.model.getSetting("smoothScroll") ) {
+ speed = 0;
+ }
+
+ $(document.body).animate({scrollTop : top, scrollLeft : left}, speed);
+ return $(this);
+ }
+});
+
vichrome.widgets.Surface = function() {
var $statusLine, initialized = false;
@@ -69,23 +151,25 @@ vichrome.widgets.Surface = function() {
if( !initialized ) {
return;
}
- $('form input:text:visible').get(0).focus();
+ $('form input:text:visible').scrollTo().get(0).focus();
return this;
};
this.scrollBy = function( x, y ) {
if( !initialized ) {
return;
}
- window.scrollBy( x, y );
+ $(document.body).scrollBy(x, y, 40);
+
return this;
};
this.scrollTo = function( x, y ) {
if( !initialized ) {
return;
}
- window.scrollTo( x, y );
+ $(document.body).scrollTo(x, y, 80);
+
return this;
};

0 comments on commit 21e43df

Please sign in to comment.
Something went wrong with that request. Please try again.