Browsersync not syncing scroll

旧巷老猫 提交于 2021-02-20 04:31:11

问题


I am new to gulp and I'm trying to get browser-sync working. It seems to work fine except that the syncronised scroll is not working. Can anyone see what might be wrong with my setup.

var gulp = require('gulp'),
    jade = require('gulp-jade'),
    uglify = require('gulp-uglify'),
    gutil = require('gulp-util'),
    sass = require('gulp-sass'),
    livereload = require('gulp-livereload'),
    browserSync = require('browser-sync');

var outputDir = "builds/development";

gulp.task('jade', function() {
    return  gulp.src('src/templates/**/*.jade')
        .pipe(jade())
        .pipe(gulp.dest(outputDir))
        .pipe(livereload());
});

// Js - to uglify use gulp js --type production ( or just leave out js to build everything )
gulp.task('js', function() {
    return gulp.src('src/js/main.js')
        .pipe(gutil.env.type === 'production' ? uglify() : gutil.noop())
        .pipe(gulp.dest(outputDir + '/js'))
        .pipe(livereload());
});

// Sass - to compress use gulp sass --type production ( or just leave out sass to build everything )
gulp.task('sass', function() {
    var config = {};
    if ( gutil.env.type === 'production') {
        config.outputStyle = 'compressed';
    }
    return gulp.src('src/sass/main.scss')
        .pipe(sass(config))
        .pipe(gulp.dest(outputDir + '/css'))
        .pipe(livereload());
});

// Browser Sync ( not working 100% ... not scrolling )
gulp.task('browser-sync', function() {
    browserSync({
        proxy: "http://localsandbox.dev/gulptutorial/builds/development/"
    });
});




// Watch
gulp.task('watch', function() {
    gulp.watch('src/templates/**/*.jade', ['jade']);
    gulp.watch('src/js/**/*.js', ['js']);
    gulp.watch('src/sass/**/*.scss', ['sass']);
});


gulp.task('default', ['jade', 'js', 'sass', 'watch', 'browser-sync'], function () {
    gulp.watch("js/*.js", ['js', browserSync.reload]);
});

回答1:


Your issue might be related to watching with both Gulp and BrowserSync. Here is a good reference to that:

"You shouldn't watch files with BrowserSync + Gulp" https://github.com/shakyShane/gulp-browser-sync/issues/16#issuecomment-43597240

So you would probably want remove livereload (let BrowserSync handle everything) and change your default Gulp task to something like this:

gulp.task('default', ['jade', 'js', 'sass', 'browser-sync'], function () {
    gulp.watch('src/templates/**/*.jade', ['jade', browserSync.reload]);
    gulp.watch('src/js/**/*.js', ['js', browserSync.reload]);
    gulp.watch('src/sass/**/*.scss', ['sass', browserSync.reload]);
});

You could also try setting scroll explicitly (but it defaults to true anyway):

gulp.task('browser-sync', function() {
    browserSync({
        proxy: "http://localsandbox.dev/gulptutorial/builds/development/"
        ghostMode: {
          scroll: true
        }
    });
});



回答2:


I had the same problem and that was because I had a javascript function called scrollTo declared in my own code, which was interfering with browserSync code. All I needed to do is to rename that function, and the browsersync scrolling was working again.



来源:https://stackoverflow.com/questions/27766350/browsersync-not-syncing-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!