gpt4 book ai didi

javascript - HTML/CSS 根据文本长度改变 anchor

转载 作者:太空宇宙 更新时间:2023-11-03 22:58:29 26 4
gpt4 key购买 nike

我最近开始学习编写一些 HTML 和 CSS 代码,我有一个网络应用程序,其中包含一些我不知道如何编写代码或找不到如何编写代码的正确方向(我似乎无法匹配我搜索中的任何内容,这就是我想要的)。

我希望根据用户名中的字符长度扩展或缩短 anchor (img) 的宽度。

我知道 CSS 不能靠自己做到这一点。我相信 JQuery 或 Javascript 可以处理这项任务,但我不确定如何去做或从哪里开始。

//我对 Javascript 和 JQuery 的理解有点有限。因此,到目前为止,我最好的方向是创建一个与用户名字符串的 .length 相匹配的 div,或者创建一个将 anchor 宽度更改为 username.length 字符串的函数。可能有以下效果:

$('anchor').ready(function() {  var anchorWidth = $('username').text().length; });

这是进入的正确方向吗?还是我偏离了目标?

编辑

>HTML code:
</head>
<body class="bodyHome">
<div class="settingMenu">
<div onclick="myFunction()">
<img class="fork" src="/Users/liamfox/Documents/Projects/PHM/SRC/fork.png" alt="settings and menu">
</div>
<div id="myDropdown" class="setting-content">
<ul class="ContentList">
<li><a href="#"">log out</a></li>
<li><a href="#"">USERNAME</a>
<img style="width: 150px;" src="/Users/liamfox/Documents/Projects/PHM/SRC/line0.png"></li>
</ul>
</div> <!-- END .settingsMenu -->
</div> <!-- END .setting-content -->

<div class="foot-signUp">
<div class="foot-login">
<div class="homeText" alt="username">
<a style="text-decoration: none; color: white;" href="#">add ingredient</a>
</div>
<a>
<img class="barLine2" src="/Users/liamfox/Documents/Projects/PHM/SRC/line0.png">
</a>

<!-- this script is to manipulate the width of the line anchor under the USERNAME to match the character length -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">
$('imgLength').ready(function () {
var matchLength = this.text().length;
if (matchLength = this.text.length) {
this.width(text.width);
}
});
</script>
</body>

到目前为止,我的脚本现在看起来更像这样:

            $(document).ready(function () {
$('.js-dropdown').on('click', function () {
var $underline = $('.js-underline');
var $userNameLength = $('.js-userNameLength');

console.log($underline);
console.log($userNameLength);

$underline.css('width', $userNameLength.width());
});
});

anchor img 可以是边框,我想通了。该图像只是我制作的一条条线,具有我选择的渐变颜色。

最佳答案

让我们使用 css 方法并应用背景图像。这也可能会清理 HTML

.ContentList {
list-style: none;
}
.ContentList li {
padding: 5px;
0
}
.ContentList a {
text-decoration: none;
font-size: 16px;
}
.js-userNameLength {
background-image: url(http://lorempixel.com/80/10/);
background-repeat: no-repeat;
background-size: 100% 10px;
background-position: bottom;
padding: 10px 0;
}
<ul class="ContentList">
<li><a href="#">log out</a>
</li>
<li><a class="js-userNameLength" href="#">USERNAME</a>
</li>
<li><a class="js-userNameLength" href="#">Really long USERNAME</a>
</li>

<li><a href="#">change password</a>
</li>
<li><a href="#">my recipes</a>
</li>
<li><a href="#">terms of use</a>
</li>
<li><a href="#">need help/feedback</a>
</li>
</ul>

在这个例子中,我假设您的线条图像的高度为 10 像素。您需要调整 .js-userNameLength 中的值以匹配您的实际图像高度。 padding 用于给图像留出空间,同样,根据需要进行调整。

您还可以使用 CSS 渐变实现无图像方法:

.ContentList {
list-style: none;
}
.ContentList li {
padding: 5px;
0
}
.ContentList a {
text-decoration: none;
font-size: 16px;
}
.js-userNameLength {
padding: 5px 0;
/* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#1e5799+85,2989d8+93,207cca+94,207cca+94,7db9e8+100&0+0,0+83,1+85,1+100;border-bottom */
/* IE9 SVG, needs conditional override of 'filter' to 'none' */
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzFlNTc5OSIgc3RvcC1vcGFjaXR5PSIwIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjgzJSIgc3RvcC1jb2xvcj0iIzFlNTc5OSIgc3RvcC1vcGFjaXR5PSIwIi8+CiAgICA8c3RvcCBvZmZzZXQ9Ijg1JSIgc3RvcC1jb2xvcj0iIzFlNTc5OSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjkzJSIgc3RvcC1jb2xvcj0iIzI5ODlkOCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9Ijk0JSIgc3RvcC1jb2xvcj0iIzIwN2NjYSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiM3ZGI5ZTgiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
background: -moz-linear-gradient(top, rgba(30, 87, 153, 0) 0%, rgba(30, 87, 153, 0) 83%, rgba(30, 87, 153, 1) 85%, rgba(41, 137, 216, 1) 93%, rgba(32, 124, 202, 1) 94%, rgba(125, 185, 232, 1) 100%);
/* FF3.6-15 */
background: -webkit-linear-gradient(top, rgba(30, 87, 153, 0) 0%, rgba(30, 87, 153, 0) 83%, rgba(30, 87, 153, 1) 85%, rgba(41, 137, 216, 1) 93%, rgba(32, 124, 202, 1) 94%, rgba(125, 185, 232, 1) 100%);
/* Chrome10-25,Safari5.1-6 */
background: linear-gradient(to bottom, rgba(30, 87, 153, 0) 0%, rgba(30, 87, 153, 0) 83%, rgba(30, 87, 153, 1) 85%, rgba(41, 137, 216, 1) 93%, rgba(32, 124, 202, 1) 94%, rgba(125, 185, 232, 1) 100%);
/* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#001e5799', endColorstr='#7db9e8', GradientType=0);
/* IE6-8 */
}
<ul class="ContentList">
<li><a href="#">log out</a>
</li>
<li><a class="js-userNameLength" href="#">USERNAME</a>
</li>
<li><a class="js-userNameLength" href="#">Really long USERNAME</a>
</li>

<li><a href="#">change password</a>
</li>
<li><a href="#">my recipes</a>
</li>
<li><a href="#">terms of use</a>
</li>
<li><a href="#">need help/feedback</a>
</li>
</ul>

CSS 渐变可能有点吓人。我使用这个很棒的工具来生成这个:http://www.colorzilla.com/gradient-editor/#1e5799+85,2989d8+93,207cca+94,207cca+94,7db9e8+100&0+0,0+83

关于javascript - HTML/CSS 根据文本长度改变 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37381695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com