【问题标题】:window resize if and else if state jquery not working如果状态 jquery 不起作用,则窗口调整大小
【发布时间】:2018-03-30 09:30:05
【问题描述】:

我想根据窗口调整大小运行脚本。当我在浏览器调整大小时应用 if 语句不起作用时。当前代码仅在我在每个屏幕中重新加载页面时才起作用。我需要在 jquery 中使用 if 和 else if 媒体查询在没有页面加载的情况下运行脚本。任何人都可以帮助我实现这一目标。

my webiste

    $(window).resize(function() {
    if ($(window).width() > 1200) {
        $('.menus-area .posts .tileh4 span:first-child').each(function() {
            var words = $(this).text();
            var maxWords = 22;
            if (words.length > maxWords) {
                html = words.slice(0, maxWords) + '<span class="more_text" style="display:none;">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"><i class="fa fa-arrow-right"></i></a>'
                $(this).html(html)
                $(this).find('a.read_more_sec').click(function(event) {
                    $(this).toggleClass("less");
                    event.preventDefault();
                    if ($(this).hasClass("less")) {
                        $(this).html("<i class='fa fa-arrow-left'></i>")
                        $(this).parent().find(".more_text").show();
                    } else {
                        $(this).html("<i class='fa fa-arrow-right'></i>")
                        $(this).parent().find(".more_text").hide();
                    }
                })
            }
        });
    }
    if ($(window).width() < 1199) {
        $('.menus-area .posts .tileh4 span:first-child').each(function() {
            var words = $(this).text();
            var maxWords = 15;
            if (words.length > maxWords) {
                html = words.slice(0, maxWords) + '<span class="more_text" style="display:none;">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"><i class="fa fa-arrow-right"></i></a>'
                $(this).html(html)
                $(this).find('a.read_more_sec').click(function(event) {
                    $(this).toggleClass("less");
                    event.preventDefault();
                    if ($(this).hasClass("less")) {
                        $(this).html("<i class='fa fa-arrow-left'></i>")
                        $(this).parent().find(".more_text").show();
                    } else {
                        $(this).html("<i class='fa fa-arrow-right'></i>")
                        $(this).parent().find(".more_text").hide();
                    }
                })
            }
        });
    }
    if ($(window).width() < 991) {
        $('.menus-area .posts .tileh4 span:first-child').each(function() {
            var words = $(this).text();
            var maxWords = 10;
            if (words.length > maxWords) {
                html = words.slice(0, maxWords) + '<span class="more_text" style="display:none;">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"><i class="fa fa-arrow-right"></i></a>'
                $(this).html(html)
                $(this).find('a.read_more_sec').click(function(event) {
                    $(this).toggleClass("less");
                    event.preventDefault();
                    if ($(this).hasClass("less")) {
                        $(this).html("<i class='fa fa-arrow-left'></i>")
                        $(this).parent().find(".more_text").show();
                    } else {
                        $(this).html("<i class='fa fa-arrow-right'></i>")
                        $(this).parent().find(".more_text").hide();
                    }
                })
            }
        });
    }
    if ($(window).width() < 639) {
        $('.menus-area .posts .tileh4 span:first-child').each(function() {
            var words = $(this).text();
            var maxWords = 10;
            if (words.length > maxWords) {
                html = words.slice(0, maxWords) + '<span class="more_text">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"></a>'
                $(this).html(html)
                $(this).find('a.read_more_sec').click(function(event) {
                    $(this).toggleClass("less");
                    event.preventDefault();
                    if ($(this).hasClass("less")) {
                        $(this).html("")
                        $(this).parent().find(".more_text").show();
                    } else {
                        $(this).html("")
                        $(this).parent().find(".more_text").hide();
                    }
                })
            }
        });
    }
});

【问题讨论】:

  • 您遇到什么错误?如果您的脚本在调整大小时执行,您是否通过打印消息来检查控制台?
  • 你有一个 });在您网站上不应该存在的脚本末尾。
  • 当我根据媒体查询调整脚本大小时不起作用。
  • @OnyxCaldin 哪个地方
  • @John 就在 $(window).on('resize', function() { ..... });

标签: javascript jquery html


【解决方案1】:

修复控制台显示的语法错误:(检查this answer是否有效)

SyntaxError: Invalid character '\u200b'

另一个answer here 讨论了这个零宽度字符是如何潜入的。

此外,表单 HTML 删除以下 onload 处理程序。

<body onload="$(window).resize()">

改为使用单个事件处理程序来绑定 loadresize 事件。

$(window).on('load resize', function(){
    var width = $(window).width();
    console.log('width', width);
})

【讨论】:

  • 你能用我上面的脚本发布完整的答案吗
  • @John 如果有不清楚的地方,请提出具体问题。
【解决方案2】:

我们需要在文档准备和窗口调整大小期间触发相同的功能。试试下面的代码

$(document).ready(function() {
    processMenuArea($);

    $(window).resize(function() {
        processMenuArea($);
    });
});

function processMenuArea($) {
    if ($(window).width() < 639) {
        $('.menus-area .posts .tileh4 span:first-child').each(function() {
            var words = $(this).text();
            var maxWords = 10;
            if (words.length > maxWords) {
                html = words.slice(0, maxWords) + '<span class="more_text">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"></a>'
                $(this).html(html)
                $(this).find('a.read_more_sec').click(function(event) {
                    $(this).toggleClass("less");
                    event.preventDefault();
                    if ($(this).hasClass("less")) {
                        $(this).html("")
                        $(this).parent().find(".more_text").show();
                    } else {
                        $(this).html("")
                        $(this).parent().find(".more_text").hide();
                    }
                })
            }
        });
    } else if ($(window).width() < 991) {
        $('.menus-area .posts .tileh4 span:first-child').each(function() {
            var words = $(this).text();
            var maxWords = 10;
            if (words.length > maxWords) {
                html = words.slice(0, maxWords) + '<span class="more_text" style="display:none;">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"><i class="fa fa-arrow-right"></i></a>'
                $(this).html(html)
                $(this).find('a.read_more_sec').click(function(event) {
                    $(this).toggleClass("less");
                    event.preventDefault();
                    if ($(this).hasClass("less")) {
                        $(this).html("<i class='fa fa-arrow-left'></i>")
                        $(this).parent().find(".more_text").show();
                    } else {
                        $(this).html("<i class='fa fa-arrow-right'></i>")
                        $(this).parent().find(".more_text").hide();
                    }
                })
            }
        });
    } else if ($(window).width() < 1199) {
        $('.menus-area .posts .tileh4 span:first-child').each(function() {
            var words = $(this).text();
            var maxWords = 15;
            if (words.length > maxWords) {
                html = words.slice(0, maxWords) + '<span class="more_text" style="display:none;">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"><i class="fa fa-arrow-right"></i></a>'
                $(this).html(html)
                $(this).find('a.read_more_sec').click(function(event) {
                    $(this).toggleClass("less");
                    event.preventDefault();
                    if ($(this).hasClass("less")) {
                        $(this).html("<i class='fa fa-arrow-left'></i>")
                        $(this).parent().find(".more_text").show();
                    } else {
                        $(this).html("<i class='fa fa-arrow-right'></i>")
                        $(this).parent().find(".more_text").hide();
                    }
                })
            }
        });
    } else {
        $('.menus-area .posts .tileh4 span:first-child').each(function() {
            var words = $(this).text();
            var maxWords = 22;
            if (words.length > maxWords) {
                html = words.slice(0, maxWords) + '<span class="more_text" style="display:none;">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"><i class="fa fa-arrow-right"></i></a>'
                $(this).html(html)
                $(this).find('a.read_more_sec').click(function(event) {
                    $(this).toggleClass("less");
                    event.preventDefault();
                    if ($(this).hasClass("less")) {
                        $(this).html("<i class='fa fa-arrow-left'></i>")
                        $(this).parent().find(".more_text").show();
                    } else {
                        $(this).html("<i class='fa fa-arrow-right'></i>")
                        $(this).parent().find(".more_text").hide();
                    }
                })
            }
        });
    }
}

【讨论】:

  • 嗨,John,我在您网站页面的控制台中运行了上述代码,我发现它在调整大小时工作正常。你能解释更多关于什么不工作以及你在任何设备上测试...
  • 不,Shreysa 先生仍然无法工作。无论如何感谢您的努力。我已经找到解决方案了。
【解决方案3】:

我试过用这个。它对我有用

$(window).on("load resize", function(e) {
        $('a.read_more_sec').each(function() {
            $(this).remove()
        });
        $('.menus-area .posts .tileh4 > span').not('.vhide,.versn').each(function() {
            var text = $(this).text();
            if ($(this).find(".vhide").length == 0) {
                $(this).html('');
                $(this).text($.trim(text))
            }
        });
        if ($(window).width() > 1200) {
            $('.menus-area .posts .tileh4 > span').not('.vhide,.versn').each(function() {
                var words = $(this).text();
                var maxWords = 21;
                if (words.length > maxWords) {
                    html = words.slice(0, maxWords) + '<span class="more_text" style="display:none;">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"><i class="fa fa-arrow-right"></i></a>'
                    $(this).html(html)
                    $(this).find('a.read_more_sec').click(function(event) {
                        $(this).toggleClass("less");
                        event.preventDefault();
                        if ($(this).hasClass("less")) {
                            $(this).html("<i class='fa fa-arrow-left'></i>")
                            $(this).parent().find(".more_text").show()
                        } else {
                            $(this).html("<i class='fa fa-arrow-right'></i>")
                            $(this).parent().find(".more_text").hide()
                        }
                    })
                }
            })
        } else if ($(window).width() < 1199 && $(window).width() >= 1025) {
            $('.menus-area .posts .tileh4 > span').not('.vhide,.versn').each(function() {
                var words = $(this).text();
                var maxWords = 15;
                if (words.length > maxWords) {
                    html = words.slice(0, maxWords) + '<span class="more_text" style="display:none;">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"><i class="fa fa-arrow-right"></i></a>'
                    $(this).html(html)
                    $(this).find('a.read_more_sec').click(function(event) {
                        $(this).toggleClass("less");
                        event.preventDefault();
                        if ($(this).hasClass("less")) {
                            $(this).html("<i class='fa fa-arrow-left'></i>")
                            $(this).parent().find(".more_text").show()
                        } else {
                            $(this).html("<i class='fa fa-arrow-right'></i>")
                            $(this).parent().find(".more_text").hide()
                        }
                    })
                }
            })
        } else if ($(window).width() < 1025 && $(window).width() >= 991) {
            $('.menus-area .posts .tileh4 > span').not('.vhide,.versn').each(function() {
                var words = $(this).text();
                var maxWords = 15;
                if (words.length > maxWords) {
                    html = words.slice(0, maxWords) + '<span class="more_text" style="display:none;">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"><i class="fa fa-arrow-right"></i></a>'
                    $(this).html(html)
                    $(this).find('a.read_more_sec').click(function(event) {
                        $(this).toggleClass("less");
                        event.preventDefault();
                        if ($(this).hasClass("less")) {
                            $(this).html("<i class='fa fa-arrow-left'></i>")
                            $(this).parent().find(".more_text").show()
                        } else {
                            $(this).html("<i class='fa fa-arrow-right'></i>")
                            $(this).parent().find(".more_text").hide()
                        }
                    })
                }
            })
        } else if ($(window).width() < 991 && $(window).width() >= 767) {
            $('.menus-area .posts .tileh4 > span').not('.vhide,.versn').each(function() {
                var words = $(this).text();
                var maxWords = 9;
                if (words.length > maxWords) {
                    html = words.slice(0, maxWords) + '<span class="more_text" style="display:none;">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"><i class="fa fa-arrow-right"></i></a>'
                    $(this).html(html)
                    $(this).find('a.read_more_sec').click(function(event) {
                        $(this).toggleClass("less");
                        event.preventDefault();
                        if ($(this).hasClass("less")) {
                            $(this).html("<i class='fa fa-arrow-left'></i>")
                            $(this).parent().find(".more_text").show()
                        } else {
                            $(this).html("<i class='fa fa-arrow-right'></i>")
                            $(this).parent().find(".more_text").hide()
                        }
                    })
                }
            })
        } else if ($(window).width() < 767) {
            $('.menus-area .posts .tileh4 > span').not('.vhide,.versn').each(function() {
                var words = $(this).text();
                var maxWords = 10;
                if (words.length > maxWords) {
                    html = words.slice(0, maxWords) + '<span class="more_text">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"></a>'
                    $(this).html(html)
                    $(this).find('a.read_more_sec').click(function(event) {
                        $(this).toggleClass("less");
                        event.preventDefault();
                        if ($(this).hasClass("less")) {
                            $(this).html("")
                            $(this).parent().find(".more_text").show()
                        } else {
                            $(this).html("")
                            $(this).parent().find(".more_text").hide()
                        }
                    })
                }
            })
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 2012-04-07
    相关资源
    最近更新 更多