blackseabreathe
@blackseabreathe
brackets

Как можно сократить этот js код?

Есть такой код. Как его можно сократить чтобы не так много его было?

$(document).ready(function() {
var windowHeight = $(window).height();
$(document).on('scroll', function() {
$('.myMouse').each(function() {
var self = $(this),
height = self.offset().top + self.height();
if ($(document).scrollTop() + windowHeight >= height) {
$('#box2').show()}});});});

$(document).ready(function() {
var windowHeight = $(window).height();
$(document).on('scroll', function() {
$('.go3').each(function() {
var self = $(this),
height = self.offset().top + self.height();
if ($(document).scrollTop() + windowHeight >= height) {
$('#box3').show()}});});});

$(document).ready(function() {
var windowHeight = $(window).height();
$(document).on('scroll', function() {
$('.go4').each(function() {
var self = $(this),
height = self.offset().top + self.height();
if ($(document).scrollTop() + windowHeight >= height) {
$('#box4').show()}});});});

$(document).ready(function() {
var windowHeight = $(window).height();
$(document).on('scroll', function() {
$('.go5').each(function() {
var self = $(this),
height = self.offset().top + self.height();
if ($(document).scrollTop() + windowHeight >= height) {
$('#box5').show()}});});});

$(document).ready(function() {
var windowHeight = $(window).height();
$(document).on('scroll', function() {
$('.go6').each(function() {
var self = $(this),
height = self.offset().top + self.height();
if ($(document).scrollTop() + windowHeight >= height) {
$('#box6').show()}});});});

$(document).ready(function() {
var windowHeight = $(window).height();
$(document).on('scroll', function() {
$('.go7').each(function() {
var self = $(this),
height = self.offset().top + self.height();
if ($(document).scrollTop() + windowHeight >= height) {
$('#box7').show()}});});});

$(document).ready(function() {
var windowHeight = $(window).height();
$(document).on('scroll', function() {
$('.go8').each(function() {
var self = $(this),
height = self.offset().top + self.height();
if ($(document).scrollTop() + windowHeight >= height) {
$('#box8').show()}});});});

$(document).ready(function() {
var windowHeight = $(window).height();
$(document).on('scroll', function() {
$('.gofooter').each(function() {
var self = $(this),
height = self.offset().top + self.height();
if ($(document).scrollTop() + windowHeight >= height) {
$('footer, .line_again').show()}});});});
  • Вопрос задан
  • 274 просмотра
Решения вопроса 1
amux
@amux
alp.ac
$(document).ready(function() {
    var windowHeight = $(window).height();
    $(document).on('scroll', function() {
        $('.myMouse, .go3, .go4, .go5, .go6, .go7, .go8, .gofooter').each(function() {
            var self = $(this),
                height = self.offset().top + self.height();
            if ($(document).scrollTop() + windowHeight >= height) {
                $('#box2, #box3, #box4, #box5, #box6, #box7, #box8, footer, .line_again').show()
            }
        });
    });
});


И совсем коротко:
$(document).ready(function(){var o=$(window).height();$(document).on("scroll",function(){$(".myMouse, .go3, .go4, .go5, .go6, .go7, .go8, .gofooter").each(function(){var n=$(this),t=n.offset().top+n.height();$(document).scrollTop()+o>=t&&$("#box2, #box3, #box4, #box5, #box6, #box7, #box8, footer, .line_again").show()})})});
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
nexmean
@nexmean
погромист
Можно воспользоваться UglifyJS, но мне кажется, что этот код нужно сделать не короче, а понятней: как минимум отступы расставить.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы